Rust笔记(一)-- 环境配置

栏目: 编程语言 · Rust · 发布时间: 6年前

内容简介:Rust笔记(一)-- 环境配置

安装:

  1. Windows平台

    下载 https://static.rust-lang.org/dist/rust-1.13.0-x86_64-pc-windows-msvc.msi ,然后双击运行即可,需要选上PATH环境变量配置

其实应该下载这个版本 https://static.rust-lang.org/dist/rust-nightly-x86_64-pc-windows-gnu.msi ,后面会说为什么

  1. Linux平台
    //安装
       $ curl -sf -L https://static.rust-lang.org/rustup.sh | sh
    //卸载
      $ sudo /usr/local/lib/rustlib/uninstall.sh
    

下面命令成功表示安装成功

c:\> rustc --version
rustc 1.13.0 (2c6933acc 2016-11-07)
C:\> cargo --version
cargo 0.13.0-nightly (eca9e15 2016-11-01)

配置IDE

这里介绍的是微软的Visual Studio Code编辑器配置Rust的环境。

Visual Studio Code支持windows、 linux 、mac等,所以在不同平台配置rust都是一样的步骤。

下载地址: https://code.visualstudio.com/

启动visual studio code,ctrl+shift+x切换到插件安装页面,输入rust。

Rust笔记(一)-- 环境配置

选择安装Rusty Code插件,支持自动完成、跳转到定义、符号等等功能。

安装完成后,重新加载即可启用。

新建一个rs文件,visual studio code自动识别为rust语言。右下角有个Rust tool missing,点击后会提示插件缺少的库,选择安装即可(需要保证rust环境安装成功)

Rust笔记(一)-- 环境配置

会出现:

Executing "cargo install racer"
 Updating registry `https://github.com/rust-lang/crates.io-index`
 ···
    Finished release [optimized + debuginfo] target(s) in 335.15 secs
  Installing C:\Users\xxx\.cargo\bin\racer.exe
warning: be sure to add `C:\Users\xxx\.cargo\bin` to your PATH to be able to run the installed binaries
Executing "cargo install rustfmt"
    Updating registry `https://github.com/rust-lang/crates.io-index`
 ···
    Finished release [optimized] target(s) in 270.41 secs
  Installing C:\Users\xxx\.cargo\bin\cargo-fmt.exe
  Installing C:\Users\xxx\.cargo\bin\rustfmt.exe
warning: be sure to add `C:\Users\xxx\.cargo\bin` to your PATH to be able to run the installed binaries
Executing "cargo install rustsym"
    Updating registry `https://github.com/rust-lang/crates.io-index`
 ···
    Finished release [optimized] target(s) in 203.64 secs
  Installing C:\Users\xxx\.cargo\bin\rustsym.exe
warning: be sure to add `C:\Users\xxx\.cargo\bin` to your PATH to be able to run the installed binaries

安装完成后即可。测试一下,ok。

Rust笔记(一)-- 环境配置

编译

直接rustc xxx.rs编译程序,会出现缺少linker.exe(选择gnu版是不会出现这个错误,应该是不依赖msvc编译环境)

error: could not exec the linker `link.exe`: 系统找不到指定的文件。 (os error 2)
  |
  = note: "link.exe"

因为在Windows平台,rust编译程序需要vs c++编译工具,可以安装vs2013或者更高版本工具,更简单的方式就是下载

Microsoft Visual C++ Build Tools 2015

Windows considerations

On Windows, Rust additionally requires the C++ build tools for Visual Studio 2013 or later. The easiest way to acquire the build tools is by installing Microsoft Visual C++ Build Tools 2015 which provides just the Visual C++ build tools. Alternately, you can install Visual Studio 2015 or Visual Studio 2013 and during install select the “C++ tools”.

For further information about configuring Rust on Windows see the Windows-specific rustup documentation.

安装完之后,重新编译成功。(编译不需要重启,但是后面调试中需要重启,否则调试器异常)

Compiling hello_world v0.1.0 (file:///xxx/rust/hello_world)
  Finished debug [unoptimized + debuginfo] target(s) in 0.28 secs

生成目录

D:\xxx\hello_world>dir
 驱动器 D 中的卷没有标签。
 卷的序列号是 309A-078B

 D:\xxx\hello_world 的目录

2016/12/19  11:21    <DIR>          .
2016/12/19  11:21    <DIR>          ..
2016/12/19  10:59                 7 .gitignore
2016/12/19  11:00                47 Cargo.lock
2016/12/19  10:59                89 Cargo.toml
2016/12/19  11:21           103,424 main.exe
2016/12/19  11:21           487,424 main.pdb
2016/12/19  10:59    <DIR>          src
2016/12/19  11:00    <DIR>          target
               5 个文件        590,991 字节
               4 个目录 58,963,050,496 可用字节

在Visual Studio Code直接输入命令编译

  1. ctrl+`,打开集成终端窗口
  2. 输入rust的编译命令即可

调试

Rust目前支持使用LLDB和GDB调试,在Visual Studio Code可以安装lldb调试插件。

但是目前lldb不支持windows平台,只能在linux平台配置lldb,配置步骤如下:

依然ctrl+shift+x,然后输入rust,在列表中选择LLDB Debugger安装即可

Rust笔记(一)-- 环境配置

安装之后,重新加载窗口,调试插件生效。

打开之前的rs文件(vs code需要打开其目录作为工程目录),切换到调试窗口,点击调试开始按钮,会打开launch.json配置文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceRoot}/xxx", //main
            "args": []
        }
    ]
}

将program中xxx配置为编译后的文件名就可以进行调试了。

在windows平台,需要使用gdb进行调试

使用TDM-GCC的GDB(需要支持 Python 扩展,MinGW64的GDB貌似不支持)

  1. 下载 TDM-GCC-gdb , 不需要安装,解压后,拷贝bin、gdb64、share目录到rust安装目录,修改gdb64\bin目录中gdbinit内容,文件末尾加上

    python
    
    print "---- Loading Rust pretty-printers ----" 
    
    sys.path.insert(0, "你的rust安装目录/lib/rustlib/etc") 
    import gdb_rust_pretty_printing 
    gdb_rust_pretty_printing.register_printers(gdb) 
    
    end
    
  2. 下载rust源码, https://github.com/rust-lang/rust ,拷贝etc目录到x\rust\lib\rustlib目录

  3. 测试gdb是否安装成功

    C:\>gdb
    GNU gdb (GDB) 7.9.1
    Copyright (C) 2015 Free Software Foundation, Inc.
    ...
    ---- Loading Rust pretty-printers ----
    
  4. 在Visual Studio Code中搜搜安装native debug插件(不止支持gdb),重新加载后,打开rs文件目录,切换到调试页面,点击调试按钮,弹出调试器列表,选择gdb,然后配置好launch.json文件(同lldb),保存即可开始调试

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Debug",
                "type": "gdb",
                "request": "launch",
                "target": "./target/debug/hello_world.exe",
                "cwd": "${workspaceRoot}"
            }
        ]
    }
    

问题

  1. 在调试中遇到问题
    ---- Loading Rust pretty-printers ----
    No symbol table is loaded.  Use the "file" command.
    

经过一番周折发现是racer\rustfmt\rustsym没有安装成功,符号相关的是rustsym,

安装完成之后,依然无法识别符号,各种翻找资料,无果。

最后在一篇 英文博客 中看到别人下载的rust版本是rust-nightly-x86_64-pc-windows-gnu.msi,突然想是不是跟版本有关,因为我下载的是msvc版,编译结果符号应该也是ms的,而调试其是gdb,是不是这样就识别不了了呢,而gnu版rust正好和gdb配套(猜测),所以应该会ok。

果不其然,重新下载了 https://static.rust-lang.org/dist/rust-nightly-x86_64-pc-windows-gnu.msi ,配置之后,可以正常识别符号了。

效果图:

Rust笔记(一)-- 环境配置

gnu版的rust在配置gdb时,不用下载rust源码添加etc目录的文件

  1. Visual Studio Code左下角出现racer crashed,点击之后看到
    Racer Output: 
    RUST_SRC_PATH environment variable must be set to point to the src directory of a rust checkout. E.g. "/home/foouser/src/rust/src"
    Racer Error:
    

是因为安装racer步骤不完整,

需要将rust源码中src拷贝到rust安装目录中,然后设置环境变量

RUST_SRC_PATH = rust安装目录\src

验证racer是否成功:

c:\>racer complete std::io::B
MATCH BufReader,50,11,C:\Program Files\Rust stable MSVC 1.13\src\libstd\io\buffered.rs,Struct,pub struct BufReader<R>
MATCH BufWriter,309,11,C:\Program Files\Rust stable MSVC 1.13\src\libstd\io\buffered.rs,Struct,pub struct BufWriter<W: Write>
MATCH BufRead,1208,10,C:\Program Files\Rust stable MSVC 1.13\src\libstd\io\mod.rs,Trait,pub trait BufRead: Read
MATCH Bytes,1605,11,C:\Program Files\Rust stable MSVC 1.13\src\libstd\io\mod.rs,Struct,pub struct Bytes<R>
MATCH BufReader,50,11,.\libstd\io\buffered.rs,Struct,pub struct BufReader<R>
MATCH BufWriter,309,11,.\libstd\io\buffered.rs,Struct,pub struct BufWriter<W: Write>
MATCH BufRead,1208,10,.\libstd\io\mod.rs,Trait,pub trait BufRead: Read
MATCH Bytes,1605,11,.\libstd\io\mod.rs,Struct,pub struct Bytes<R>

在用户设置中配置如下参数:

(如果已经将rust\bin和.cargo\bin加入PATH,并且设置好了RUST_SRC_PATH的话,这一步可以省略)

{

“rust.racerPath”: null, // Specifies path to Racer binary if it’s not in PATH

“rust.rustLangSrcPath”: null, // Specifies path to /src directory of local copy of Rust sources

“rust.rustfmtPath”: null, // Specifies path to Rustfmt binary if it’s not in PATH

“rust.cargoPath”: null, // Specifies path to Cargo binary if it’s not in PATH

“rust.cargoHomePath”: null, // Path to Cargo home directory, mostly needed for racer. Needed only if using custom rust installation.

“rust.formatOnSave”: false, // Turn on/off autoformatting file on save (EXPERIMENTAL)

“rust.checkOnSave”: false, // Turn on/off cargo check project on save (EXPERIMENTAL)

“rust.checkWith”: “build” // Specifies the linter to use. (EXPERIMENTAL)

}


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

标签: rust

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python the Hard Way

Learn Python the Hard Way

Zed A. Shaw / Addison-Wesley Professional / 2013-10-11 / USD 39.99

Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called "The Hard Way," but it's re......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具