1.切换源为国内
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
2.安装rust
如果第一次提示网络错误,多试几次就好了
curl https://mirrors.ustc.edu.cn/rust-static/rustup.sh | sh -s
3.配置代理
$ vim ~/.cargo/config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
:wq
4.安装工具
由于安装racer可能报错#![feature] may not be used on the stable release channel 提前切换到nighty版本
rustup install nightly
rustup default nightly
同样,如果安装超时,多试几次 继续安装
cargo install racer
racer -V
cargo install --force rustfmt
cargo install --force rls
如果报错xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer") does not exist 可能是由于xcode卸载了,或者更新版本了,那么需要先进行重置,再执行上面的命令
sudo xcode-select -r
sudo xcode-select --switch /Library/Developer/CommandLineTools
xcode-select -p
5.组件安装
rustup component add rls-preview rust-analysis rust-src
6.安装 VSCode Rust 扩展
打开 VSCode,在扩展中心搜索 Rust,安装第一个 Rust(rls) 即可。 安装完成后需要在 VSCode 中设置 rust-client.channel 为 stable。 同时建议设置 editor.formatOnSave 为 true,以便在保存时使用 rust-fmt 格式化代码。
注意:一定不要同时有多个 Rust 扩展,需要先将其他 Rust 扩展卸载,以防出问题。
7.创建新项目用vscode测试是否可以格式化代码
cargo new test && cd test