这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Rust安装

在各个操作系统上安装Rust

Rust安装方式参考官方地址: https://www.rust-lang.org/en-US/install.html

1 - Linux 安装

在 Linux 上安装 Rust

安装

执行命令:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

然后依照屏幕提示:

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/sky/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  /home/sky/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/sky/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/sky/.profile
  /home/sky/.bashrc
  /home/sky/.zshenv

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>

这里选择1默认模式,继续:

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2025-04-03, rust version 1.86.0 (05f9846f8 2025-03-31)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 72.8 MiB /  72.8 MiB (100 %)  31.1 MiB/s in  1s         
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 21.2 MiB /  21.2 MiB (100 %)  10.2 MiB/s in  2s         
info: installing component 'rust-std'
 27.1 MiB /  27.1 MiB (100 %)  11.6 MiB/s in  2s         
info: installing component 'rustc'
 72.8 MiB /  72.8 MiB (100 %)  13.1 MiB/s in  5s         
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.86.0 (05f9846f8 2025-03-31)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.

This is usually done by running one of the following (note the leading DOT):
. "$HOME/.cargo/env"            # For sh/bash/zsh/ash/dash/pdksh
source "$HOME/.cargo/env.fish"  # For fish
source "$HOME/.cargo/env.nu"    # For nushell
vi ~/.zshrc

在文件末尾加入:

# rust
. "$HOME/.cargo/env"

保存退出, 重新登录, 或者执行命令:

source $HOME/.cargo/env

验证安装:

$ rustc --version
rustc 1.86.0 (05f9846f8 2025-03-31)

配置cargo

打开(或创建)文件 ~/.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"

参考:

2 - Windows 安装

在 Windows 上安装 Rust

安装vs2015

TBD: 下次验证,参考 https://rustlang-cn.org/office/rust/book/getting-started/ch01-01-installation.html

在安装rust之前,windows平台上需要先安装 Microsoft C++ build tools,推荐2015版本。如果不安装,后面在编译时,会报错说"link.exe"无法找到。

note: the msvc targets depend on the msvc linker but `link.exe` was not found

note: please ensure that VS 2013 or VS 2015 was installed with the Visual C++ option

error: aborting due to previous error

error: Could not compile `ws2_32-sys`

打开下面的网站:

http://landinghub.visualstudio.com/visual-cpp-build-tools

选择"Download Visual C++ Build Tools 2015"。

下载之后按照提示一路安装即可。

windows安装rust

操作系统为windows 10 64位。

下载rustup.init.exe,然后安装,按照指示操作,中间要下载rustc等安装文件。

Rust Visual C++ prerequisites

If you will be targeting the GNU ABI or otherwise know what you are doing then
it is fine to continue installation without the build tools, but otherwise,
install the C++ build tools before proceeding.

Continue? (Y/n) y


Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

  C:\Users\aoxia\.cargo\bin

This path will then be added to your PATH environment variable by modifying the
HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation


info: updating existing rustup installation


Rust is installed now. Great!

To get started you need Cargo's bin directory (%USERPROFILE%\.cargo\bin) in
your PATH environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.

Press the Enter key to continue.

%USERPROFILE%\.cargo\bin 加入到PATH环境变量中。

验证安装,执行rustc --version

$> rustc --version
rustc 1.33.0 (2aa4c46cf 2019-02-28) 

3 - Rust 版本升级

升级 Rust 的版本

官方方式

通过 rustup 安装了 Rust 之后,更新到最新版本只要运行如下更新脚本:

$ rustup update

尝试在mac下从 1.40 升级到 1.42 成功,输出如下:

$ rustup update
info: syncing channel updates for 'stable-x86_64-apple-darwin'
463.3 KiB / 463.3 KiB (100 %) 414.2 KiB/s in  1s ETA:  0s
info: latest update on 2020-03-12, rust version 1.42.0 (b8cedc004 2020-03-09)
info: downloading component 'rust-src'
  2.2 MiB /   2.2 MiB (100 %) 313.6 KiB/s in  8s ETA:  0s
info: downloading component 'cargo'
  3.7 MiB /   3.7 MiB (100 %) 321.3 KiB/s in 14s ETA:  0s
info: downloading component 'clippy'
  1.3 MiB /   1.3 MiB (100 %) 327.7 KiB/s in  5s ETA:  0s
info: downloading component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %) 323.2 KiB/s in 43s ETA:  0s
info: downloading component 'rust-std'
 16.1 MiB /  16.1 MiB (100 %) 345.6 KiB/s in  1m  3s ETA:  0s
info: downloading component 'rustc'
 54.5 MiB /  54.5 MiB (100 %) 342.4 KiB/s in  4m 20s ETA:  0s    
info: downloading component 'rustfmt'
  1.9 MiB /   1.9 MiB (100 %) 288.0 KiB/s in  7s ETA:  0s
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 12.1 MiB /  12.1 MiB (100 %)   8.6 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
info: installing component 'rustc'
 54.5 MiB /  54.5 MiB (100 %)  18.3 MiB/s in  3s ETA:  0s
info: installing component 'rustfmt'
info: checking for self-updates

  stable-x86_64-apple-darwin updated - rustc 1.42.0 (b8cedc004 2020-03-09) (from rustc 1.40.0 (73528e339 2019-12-16))

info: cleaning up downloads & tmp directories

尝试在linux 下从 1.54.0 升级到 1.55.0 成功,输出如下:

➜  ~ rustup update
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'

679.5 KiB / 679.5 KiB (100 %) 463.5 KiB/s in  1s ETA:  0s
info: latest update on 2021-09-09, rust version 1.55.0 (c8dfcfe04 2021-09-06)
info: downloading component 'rust-src'
info: downloading component 'cargo'
  6.1 MiB /   6.1 MiB (100 %)   4.4 MiB/s in  1s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 17.0 MiB /  17.0 MiB (100 %)   4.5 MiB/s in  3s ETA:  0s
info: downloading component 'rust-std'
 22.3 MiB /  22.3 MiB (100 %)   4.6 MiB/s in  5s ETA:  0s
info: downloading component 'rustc'
 51.0 MiB /  51.0 MiB (100 %)   4.2 MiB/s in 11s ETA:  0s
info: downloading component 'rustfmt'
info: removing previous version of component 'rust-src'
info: removing previous version of component 'cargo'
info: removing previous version of component 'clippy'
info: removing previous version of component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'rustfmt'
info: installing component 'rust-src'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 17.0 MiB /  17.0 MiB (100 %)  11.3 MiB/s in  1s ETA:  0s
info: installing component 'rust-std'
 22.3 MiB /  22.3 MiB (100 %)  16.7 MiB/s in  1s ETA:  0s
info: installing component 'rustc'
 51.0 MiB /  51.0 MiB (100 %)  18.9 MiB/s in  2s ETA:  0s
info: installing component 'rustfmt'
info: checking for self-updates

  stable-x86_64-unknown-linux-gnu updated - rustc 1.55.0 (c8dfcfe04 2021-09-06) (from rustc 1.54.0 (a178d0322 2021-07-26))

info: cleaning up downloads & tmp directories

如果要卸载 Rust 和 rustup,运行如下卸载脚本:

$ rustup self uninstall

归档内容:曾经遇到的升级问题

尝试过升级已经安装的rust/cargo,试图从1.22.1升级到1.23,发现重新运行rustup脚本,虽然报告说安装成功,但是实际不会安装新的版本。

暂时没有找到升级的方法,只好用最笨的办法,先删除再全新安装:

cd
rm -rf .cargo/ .rustup/

这个方式理所当然的很不好,原有的所有内容都要重头来一次。

后面似乎没有再遇到类似问题。

4 - cargo 设置

为 Rust 配置 cargo

为了加速 cargo 的构建,最好配置 cargo 的源为国内的源,或者使用 nexus 的代理源。

使用国内源

vi ~/.cargo/config

打开(或创建)配置文件 .cargo/config,加入以下内容:

[source.crates-io]
# 指定使用下面哪个源,修改为source.后面的内容即可
replace-with = 'ustc'

#阿里云
[source.aliyun]
registry = "sparse+https://mirrors.aliyun.com/crates.io-index/"

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

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

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

# rustcc社区
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

参考:

使用 nexus 代理源

nexus 支持 cargo 的代理, 具体可以参考:

但这个文档实在太过简陋, 很多地方没有说清楚, 所以这里详细说明下。

配置 nexus 代理源

在 nexus 下创建一个 cargo proxy 代理仓库,用于官方仓库:

注意: 只能代理官方仓库 cargo-proxy-cratesio, 不能代理国内的源,下面有说明。

再创建一个 cargo hosted 仓库

  • cargo-hosted

最后再创建一个 cargo group 仓库, 名为 cargo-all, 将上面两个仓库添加进去,顺序为:

  • cargo-hosted
  • cargo-proxy-cratesio

配置 cargo 使用 nexus 代理源

vi ~/.cargo/config

打开(或创建)配置文件 .cargo/config.toml,加入以下内容:

[registries.nexus]
index = "sparse+http://192.168.0.246:8081/repository/cargo-proxy-all/"

[registry]
default = "nexus"

[source.crates-io]
replace-with = "nexus"

[source.nexus]
registry = "sparse+http://192.168.0.246:8081/repository/cargo-proxy-all/"

参考:

现有问题

但这里有个问题,就是 cargo proxy 仓库只能代理官方仓库 https://index.crates.io, 而不能代理国内的源,

会报错:

warning: spurious network error (2 tries remaining): failed to get successful HTTP response from `http://192.168.0.246:8081/repository/cargo-proxy-ustc/crates/async-recursion/1.1.1/download` (192.168.0.246), got 500
body:

暂时没有找到解决办法, 所以只能使用官方仓库作为 cargo proxy 仓库的上游源头。

验证构建

验证 cargo proxy 仓库

尝试构建 linkerd2-proxy, 验证是否能正常构建:

git clone https://github.com/linkerd/linkerd2-proxy.git
cd linkerd2-proxy

# 确保安装了以下依赖
sudo apt install -y cmake make gcc g++
sudo apt install -y clang libclang-dev

# 设置环境变量
export RUSTFLAGS="--cfg tokio_unstable"

# 构建
cargo build

代理成功之后, 下载 cargo crates 的速度会非常快, cargo 代理仓库的内容如下图所示:

清理 cache 和 index

为了多次验证, 需要清理 cache 和 index:

# 第一次运行时需要安装 cargo-cache  
cargo install cargo-cache

# 清理 cache 和 index
cargo cache -a

# 清理 registry 索引
rm -rf ~/.cargo/registry/index/*

# 清理 git 索引
rm -rf ~/.cargo/git/*

# 清理 build 缓存
cargo clean

遇到特殊情况,可以考虑重置 cargo 的配置:

rm -rf ~/.cargo/
# 重新安装 rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 重新初始化 Cargo
rustup update

验证 cargo hosted 仓库

验证 cargo hosted 仓库是否能正常构建:

cargo publish

TODO:稍后再验证吧,好像一般也很少需要使用 cargo hosted 仓库。