Rust镜像源推荐及使用指南

最近在学习Rust编程,但官方源下载速度很不稳定。请问国内有哪些可靠的Rust镜像源推荐?具体应该怎么配置才能使用这些镜像源?有没有针对不同操作系统的详细配置指南?另外使用镜像源后还需要注意哪些问题?

2 回复

推荐使用中科大、清华或阿里云的Rust镜像源。以中科大的源为例,在~/.cargo/config中添加:

[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

然后运行cargo build即可。


为提升Rust开发效率,以下是国内主流镜像源配置及使用指南:

推荐镜像源

  1. 中科大(稳定推荐)
https://mirrors.ustc.edu.cn/crates.io-index/
  1. 清华大学
https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git
  1. 上海交大
https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index/

配置方法

  1. 创建/编辑cargo配置文件:
# Linux/MacOS
vim ~/.cargo/config

# Windows
notepad %USERPROFILE%\.cargo\config
  1. 写入配置(以中科大为示例):
[registry]
index = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"

[source.crates-io]
replace-with = 'ustc'

[source.ustc]
registry = "sparse+https://mirrors.ustc.edu.cn/crates.io-index/"

# 如需使用本地私有源可添加
[source.private]
registry = "file:///path/to/private-registry"

环境变量配置(备选方案)

export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
export CARGO_REGISTRIES_CRATES_IO_INDEX="https://mirrors.ustc.edu.cn/crates.io-index/"

验证配置 执行以下命令测试下载速度:

cargo new test-project && cd test-project
cargo add rand  # 观察依赖下载速度

注意事项

  1. 使用 sparse 协议(URL以 sparse+ 开头)可显著提升性能
  2. 首次使用建议执行 cargo search tokio 测试连接
  3. 如遇同步延迟可临时切换回官方源:
[source.crates-io]
replace-with = 'original'

[source.original]
registry = "https://github.com/rust-lang/crates.io-index"

建议优先选择中科大源,其同步频率高且稳定性最佳。配置完成后可体验接近本地网络的依赖下载速度。

回到顶部