使用国内源安装 Rust

更新日期: 2024-04-27 阅读次数: 2874 字数: 998 分类: rust

这段时间写 Android 界面快吐了,极度枯燥,想学习一下 Rust 放松一下心情。于是在 Windows 11 的 WSL Ubuntu 中安装最新版本的 Rust。

下载安装脚本

参考 Rust 官网的新手文档

https://www.rust-lang.org/learn/get-started

在终端命令行中执行

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

但是这个安装过程非常慢,且没有进度条,估计是下载速度不行。

参照网上的做法,可以先下载官方的安装脚本,然后将其中的下载源替换为国内的源即可。

所以,先保存 Rust 官方的安装脚本:

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

替换国内源

VIM 打开 rust.sh,替换 RUSTUP_UPDATE_ROOT 为:

RUSTUP_UPDATE_ROOT="https://mirrors.ustc.edu.cn/rust-static/rustup"

保存退出。

然后修改环境变量:

export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup

安装

给安装脚本加上可执行权限,然后执行即可开始下载并安装 Rust:

chmod +x rust.sh
./rust.sh

首先会看到提示:

Current installation options:


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

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

这里使用默认设置,敲回车即可。

安装日志

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 2023-10-05, rust version 1.73.0 (cc66ad468 2023-10-03)
info: downloading component 'cargo'
  7.8 MiB /   7.8 MiB (100 %)   4.5 MiB/s in  7s ETA:  0s
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 13.8 MiB /  13.8 MiB (100 %)   4.3 MiB/s in  3s ETA:  0s
info: downloading component 'rust-std'
 24.7 MiB /  24.7 MiB (100 %)   5.6 MiB/s in  5s ETA:  0s
info: downloading component 'rustc'
 61.6 MiB /  61.6 MiB (100 %)   5.7 MiB/s in 17s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 13.8 MiB /  13.8 MiB (100 %)   1.1 MiB/s in  1m 29s ETA:  0s
info: installing component 'rust-std'
 24.7 MiB /  24.7 MiB (100 %)  14.2 MiB/s in  2s ETA:  0s
 13 IO-ops /  13 IO-ops (100 %)  12 IOPS in  1s ETA:  0s
info: installing component 'rustc'
 61.6 MiB /  61.6 MiB (100 %)  16.9 MiB/s in  3s ETA:  0s
 16 IO-ops /  16 IO-ops (100 %)   3 IOPS in 10s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

stable-x86_64-unknown-linux-gnu installed - rustc 1.73.0 (cc66ad468 2023-10-03)

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, run:
source "$HOME/.cargo/env"

查看版本

退出终端,重新打开终端:

$ cargo --version
cargo 1.73.0 (9c4383fb5 2023-08-26)

可以看到版本号,说明安装成功。

注:cargo 是官方的 Rust 包管理工具。

Cargo,中文意思为“(船或飞机装载的)货物” 等

hello world

新建一个 Rust 项目

> cargo new hello
> cd hello/
> tree
.
├── Cargo.toml
└── src
    └── main.rs

cargo new 很方便,很遗憾 golang 一直没有在 go 工具中直接支持。

默认生成的 main.rs 里包含了 hello world 代码:

> cat src/main.rs
fn main() {
    println!("Hello, world!");
}

运行一下:

> cargo run
   Compiling hello v0.1.0 (/home/zhongwei/work/temp/hello)
    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
     Running `target/debug/hello`
Hello, world!

编译后的文件大小

编译:

> cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.01s

编译速度很快,秒完成。

查看可执行文件的大小

> ls -lah ./target/debug/hello
4.5M Oct 31 10:59 ./target/debug/hello

cargo run / build 时依赖包下载慢

虽然 cargo 已顺利安装,但是之后在项目中安装三方依赖包,还是慢,估计是国内源没有写入配置文件的缘故。

参考:

https://mirrors.tuna.tsinghua.edu.cn/help/rustup/

若要长期启用镜像源,执行:

# for bash
echo 'export RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup' >> ~/.bash_profile
echo 'export RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup' >> ~/.bash_profile

# for fish
echo 'set -x RUSTUP_UPDATE_ROOT https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup' >> ~/.config/fish/config.fish
echo 'set -x RUSTUP_DIST_SERVER https://mirrors.tuna.tsinghua.edu.cn/rustup' >> ~/.config/fish/config.fish

配置文件

~/.cargo/config

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
# 指定镜像
replace-with = 'tuna' # 如:tuna、sjtu、ustc,或者 rustcc

# 注:以下源配置一个即可,无需全部

# 中国科学技术大学
[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"

查看合集

📖 Rust web 框架 axum 教程:从入门到遥遥领先

参考

  • https://www.rust-lang.org/learn/get-started
  • https://www.jianshu.com/p/105a814e7a84
  • https://mirrors.tuna.tsinghua.edu.cn/help/rustup/

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式