1. 使用国内镜像源(推荐)
设置环境变量(临时)
# 设置 RUSTUP_DIST_SERVER(更新源) set RUSTUP_DIST_SERVER=https://rsproxy.cn set RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup # 设置 CARGO_REGISTRY(包注册源) set CARGO_REGISTRY=https://rsproxy.cn/crates.io-index # 然后运行更新 rustup update
设置环境变量(永久)
Windows (PowerShell):
# 添加到系统环境变量 [System.Environment]::SetEnvironmentVariable('RUSTUP_DIST_SERVER', 'https://rsproxy.cn', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('RUSTUP_UPDATE_ROOT', 'https://rsproxy.cn/rustup', [System.EnvironmentVariableTarget]::User) [System.Environment]::SetEnvironmentVariable('CARGO_REGISTRY', 'https://rsproxy.cn/crates.io-index', [System.EnvironmentVariableTarget]::User) # 重启终端后运行 rustup update
Linux/macOS:
# 添加到 ~/.bashrc 或 ~/.zshrc echo 'export RUSTUP_DIST_SERVER=https://rsproxy.cn' >> ~/.bashrc echo 'export RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup' >> ~/.bashrc echo 'export CARGO_REGISTRY=https://rsproxy.cn/crates.io-index' >> ~/.bashrc # 重新加载配置 source ~/.bashrc # 然后更新 rustup update
2. 可用的国内镜像源
# 清华大学镜像源 set RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup set RUSTUP_UPDATE_ROOT=https://mirrors.tuna.tsinghua.edu.cn/rustup/rustup # 中国科学技术大学镜像源 set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup # 上海交通大学镜像源 set RUSTUP_DIST_SERVER=https://mirrors.sjtug.sjtu.edu.cn/rust-static/ set RUSTUP_UPDATE_ROOT=https://mirrors.sjtug.sjtu.edu.cn/rust-static/rustup/
3. 修改 Rustup 配置文件
编辑 ~/.rustup/settings.toml
(Linux/macOS)或 %USERPROFILE%\.rustup\settings.toml
(Windows):
default_host_triple = "x86_64-pc-windows-msvc" profile = "default" version = "12" [overrides] [telemetry] disabled = false # 添加镜像配置 [dist] root = "https://rsproxy.cn" update-root = "https://rsproxy.cn/rustup"
4. 使用代理
如果你有网络代理,可以设置代理:
# 设置 HTTP 代理 set http_proxy=http://127.0.0.1:1080 set https_proxy=http://127.0.0.1:1080 # 或者使用 socks5 代理 set all_proxy=socks5://127.0.0.1:1080
5. 手动下载更新
如果网络实在无法连接,可以手动下载:
-
访问 https://forge.rust-lang.org/infra/other-installation-methods.html
-
下载对应平台的安装包
-
运行安装程序
6. 使用 cargo 镜像加速
创建或编辑 ~/.cargo/config
文件:
[source.crates-io] replace-with = 'rsproxy' [source.rsproxy] registry = "https://rsproxy.cn/crates.io-index" [source.rsproxy-sparse] registry = "sparse+https://rsproxy.cn/index/" [registries.rsproxy] index = "https://rsproxy.cn/crates.io-index" [net] git-fetch-with-cli = true
7. 检查网络连接
# 测试网络连接 curl -I https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/stable # 或者使用 ping ping static.rust-lang.org
8. 完全重新安装
如果更新一直失败,可以考虑完全重新安装:
# 卸载当前版本 rustup self uninstall # 重新安装 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain stable -y # 或者使用国内镜像安装 curl -sSf https://rsproxy.cn/rustup-init.sh | sh -s -- --default-toolchain stable -y
推荐方案
首选方案1:使用 rsproxy.cn 镜像源,这是 Rust 官方推荐的国内镜像。
如果还是慢,可以尝试不同的镜像源,或者使用代理方案。
记得在更新完成后,可以恢复原来的设置,或者保留镜像源以获得更快的下载速度。