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版本。