使用方法
编辑 $CARGO_HOME/config.toml 文件,添加以下内容:
[source.crates-io]
replace-with = 'mirror'
[source.mirror]
registry = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"
[registries.mirror]
index = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"
注:sparse+ 表示在使用稀疏索引,链接末尾的 / 不能缺少。
注:$CARGO_HOME:在 Windows 系统默认为:%USERPROFILE%\.cargo,在类 Unix 系统默认为:$HOME/.cargo。
注:cargo 仍会尝试读取不带 .toml 扩展名的配置文件(即 $CARGO_HOME/config),但从 1.39 版本起,cargo 引入了对 .toml 扩展名的支持,并将其设为首选格式。请根据使用的 cargo 版本选择适当的配置文件名。
注:使用 cargo search、cargo info 等命令时需要添加 --registry mirror,例如 cargo search --registry mirror reqwest。
在 Linux 环境可以使用下面的命令完成:
mkdir -vp ${CARGO_HOME:-$HOME/.cargo}
cat << EOF | tee -a ${CARGO_HOME:-$HOME/.cargo}/config.toml
[source.crates-io]
replace-with = 'mirror'
[source.mirror]
registry = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"
[registries.mirror]
index = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"
EOF
截至目前,可以通过 cargo +nightly -Z sparse-registry update 使用稀疏索引。
cargo 1.68 版本开始支持稀疏索引:不再需要完整克隆 crates.io-index 仓库,可以加快获取包的速度。如果您的 cargo 版本大于等于 1.68,可以直接使用而不需要开启 nightly。
评论