Rust

Rust
https://www.rust-lang.org/en-US/
https://github.com/rust-lang/rust


安装
方式一:Homebrew(mac)
$ brew install rust


方式二:rust-lang.org(linux, mac)
1. 从官网下载安装包
   https://www.rust-lang.org/en-US/downloads.html
2. 安装 rust
Mac是pkg文件、Windows是msi文件。
以linux的tar.gz包为例:
$ tar xzf rust-1.10.0-x86_64-unknown-linux-gnu.tar.gz
$ cd rust-1.10.0-x86_64-unknown-linux-gnu
$ sudo install.sh
(详细见REMDME.md文件)
$ cd ..
$ rm -rf rust-1.10.0-x86_64-unknown-linux-gnu


方式三:源码
需要手动编译 rust 和 cargo 源码。
详细见 rust 的 github 页面。


安装工具
代码提示器 racer 和编码格式化 rustfmt 工具
$ cargo install racer      # https://github.com/phildawes/racer
$ cargo install rustfmt    # https://github.com/rust-lang-nursery/rustfmt
如果网络问题,可以换时间多尝试几次,也可以到其github页面查看下载源码安装的方法

下载rust源码,racer需要rust源码
$ git clone --depth 1 https://github.com/rust-lang/rust.git

配置环境变量
RUST_SRC_PATH 配置rust源码/src路径。
如:(请根据实际情况设置src路径,并且使用三个配置文件中的哪一个)
$ echo 'export RUST_SRC_PATH="$HOME/Developer/github/rust/src"' >> ~/.bash_profile
$ echo 'export RUST_SRC_PATH="$HOME/Developer/github/rust/src"' >> ~/.bashrc        # Ubuntu Desktop
$ echo 'export RUST_SRC_PATH="$HOME/Developer/github/rust/src"' >> ~/.zshrc         # Zsh
创建软链接
$ ln -s ~/.cargo/bin/racer /usr/local/bin/racer
$ ln -s ~/.cargo/bin/rustfmt /usr/local/bin/rustfmt
$ ln -s ~/.cargo/bin/cargo-fmt /usr/local/bin/cargo-fmt
测试racer
$ racer complete std::io::B
MATCH BufReader,48,11,/Users/yk/Desktop/Developer/github/rust/src/libstd/io/buffered.rs,Struct,pub struct BufReader<R>
MATCH BufWriter,300,11,/Users/yk/Desktop/Developer/github/rust/src/libstd/io/buffered.rs,Struct,pub struct BufWriter<W: Write>
MATCH BufRead,1199,10,/Users/yk/Desktop/Developer/github/rust/src/libstd/io/mod.rs,Trait,pub trait BufRead: Read
MATCH Bytes,1532,11,/Users/yk/Desktop/Developer/github/rust/src/libstd/io/mod.rs,Struct,pub struct Bytes<R>
注意:如果使用VSCode编辑器,前面的racer配置一定要正确,否则VSCode的Rusty Code扩展无法使用racer。
创建一个helloworld $ cargo new helloworld --bin $ cd helloworld $ cargo build # 编译debug版本。release版本请加 --release 参数 $ ./target/debug/helloworld Hello, world! TextMate 打开一个rs文件,会提示安装Rust Bundle 打开一个toml文件,会提示安装Toml Bundle CMD+B编译,CMD+R运行。 Sublime Text 可利用package controller安装一些扩展: Rust # 语法高亮加强(新版Sublime Text 3自带Rust高亮,可忽略) RustAutoComplete # 利用Racer自动完成,需要根据说明配置racer和rust/src路径 BeautifyRust # 利用rustfmt格式化代码,需要根据说明配置rustfmt路径 CMD+B编译或运行,CMD+SHIFT+B更换编译或运行。 VSCode 用VSCode打开helloworld目录 安装Rusty Code扩展:CMD+SHIFT+P(或者F1),输入:ext install,搜索rusty code,安装之。 配置扩展: 在helloworld项目目录新建一个.vscode目录,然后在.vscode目录下建立settings.json 内容如下: {
"rust.racerPath": "/usr/local/bin/racer", // 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": "/usr/local/bin/rustfmt", // 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.racerPath和rust.rustfmtPath即可,如果vscode没有代码提示,请看前面“安装工具”到“创建一个helloworld”之间的内容。 打开.gitignore文件,添加一行:.vscode 打开main.rs文件,按CMD+SHIFT+P(或者F1),输入format,选择Format Code(记住快捷键SHIFT+OPTION+F)即可格式化代码。 输入std::即可看见rancer代码提示。 按按CMD+SHIFT+P(或者F1),输入cargo,可看见一组cargo命令,其中CTRL+SHIFT+B编译debug版,CTRL+SHIFT+R运行debug版, 快捷键加option是release版本。