Rust 安装与源加速实践
韦世东的技术专栏 2021-01-25 Rust
Rust 官方建议通过 RustUp 来安装和管理 Rust,官方给出的安装命令为:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
1
# 源加速
不过由于网络问题,基本上是下载不了的。这时候就需要借助国内一些开源的镜像服务来帮助我们顺利下载它。这里使用清华源,在终端执行如下命令:
export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
1
2
2
这两个命令的作用是指定 RustUp 的下载地址和更新地址,下次执行 curl 命令时会根据这里设定的源地址下载 RustUp。设置好后执行官方给出的安装命令:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
1
如果没有意外,下载和安装都会顺利进行。安装 Shell 会在终端提示我们选择安装模式,具体显示如下:
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
1
2
3
2
3
选项 1 代表按照默认设置安装;选项 2 代表自定义安装;选项 3 代表取消安装;这里通常选择 1 即可,在终端输入 1 后可以看到安装 Shell 的输出:
info: profile set to 'default'
info: default host triple is x86_64-apple-darwin
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2020-12-31, rust version 1.49.0 (e1884a8e3 2020-12-29)
info: downloading component 'cargo'
4.1 MiB / 4.1 MiB (100 %) 1.1 MiB/s in 4s ETA: 0s
info: downloading component 'clippy'
1.8 MiB / 1.8 MiB (100 %) 1.2 MiB/s in 1s ETA: 0s
info: downloading component 'rust-docs'
13.8 MiB / 13.8 MiB (100 %) 950.4 KiB/s in 15s ETA: 0s
info: downloading component 'rust-std'
21.1 MiB / 21.1 MiB (100 %) 934.4 KiB/s in 23s ETA: 0s
info: downloading component 'rustc'
50.8 MiB / 50.8 MiB (100 %) 950.4 KiB/s in 55s ETA: 0s
info: downloading component 'rustfmt'
2.2 MiB / 2.2 MiB (100 %) 1.1 MiB/s in 2s ETA: 0s
info: installing component 'cargo'
info: using up to 500.0 MiB of RAM to unpack components
info: installing component 'clippy'
info: installing component 'rust-docs'
13.8 MiB / 13.8 MiB (100 %) 11.0 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
21.1 MiB / 21.1 MiB (100 %) 14.0 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
50.8 MiB / 50.8 MiB (100 %) 15.2 MiB/s in 3s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-apple-darwin'
stable-x86_64-apple-darwin installed - rustc 1.49.0 (e1884a8e3 2020-12-29)
Rust is installed now. Great!
To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done
automatically.
To configure your current shell, run:
source $HOME/.cargo/env
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
安装完成后,记得执行安装提示末尾的命令:
source $HOME/.cargo/env
1
这时候在终端输入 rustc --version 便会看到对应的版本信息,例如 rustc 1.49.0。
安装成功,你学会了吗?
# 在线运行平台
如果你只是想练习基本语法,或者在一台临时使用的电脑上编写一些简单的代码,可以考虑 Rust PlayGround,这是 Rust 团队开发的一款能够在线编写/运行/调试 Rust 代码的在线工具,执行效率也很不错的,可以前往 https://play.rust-lang.org/ 体验一番!