在各种操作系统上安装和配置Rust

1. 安装 rust

2. 在 Linux or macOS 上安装 Rust

  • 在 Linux or Mac 上安装 Rust:
1
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf| sh

3. 在 Windows 上安装 Rust

4. 配置 Rust

4.1. 配置PATh

在Rust开发环境中,所有工具都安装到~/.cargo/bin目录中,在这里您可以找到Rust工具链,包括rustc、cargo和rustup。

为了在任何目录下都能使用这些工具,需要将~/.cargo/bin添加到环境变量PATH中。

4.2. 替换镜像(linux, Windows, macOS 通用)

安装完成后需要将 crates.io 替换成国内镜像.

https://crates.io 的访问非常缓慢,github 的仓库也经常不能访问,建议大家切换到国内镜像站。镜像站实时缓存,托管在码云的 gitee 仓库每隔 30 分钟与 github 同步。

配置方式

  • 在 <home_dir>/.cargo 目录新建文件 config

注意:新安装的机器没有这个文件,需要新建,并且该文件没有后缀名

编辑文件内容:

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
[source.crates-io]
registry="https://github.com/rust-lang/crates.io-index" # 这行可以不要,只是说明原始地址

# 替换成你偏好的镜像源
replace-with = 'tuna'
#replace-with = 'ustc'

[source.crates-cn]
registry="https://gitee.com/crates/crates.io-index.git"


# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# rustcc社区
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"

[net]
git-fetch-with-cli =true

修改完保存后即可编译 Rust 项目

1
cargo build

5. 更新 Rust

1
rustup update

6. 卸载 Rust

1
rustup self uninstall

7. 安装指定版本的Rust(例如 1.56)

1
rustup install 1.56

8. 验证安装是否成功

1
rustc --version
  • 结果格式: rustc x.y. z (abcabcabc yyyy-mm-dd)
  • 会显示最新稳定版的: 版本号, commit hash, commit 日期
  • 当前我安装的版本为 rustc 1.56.1 (59eed8a2a 2021-11-01)

9. 延申阅读

Rust 编程语言入门教程

10. 参考文章

Notes about Rust installation