本文针对的是Cargo 1.37版本。
Rust项目的依赖主要在Cargo.toml文件[dependencies]段落中定义,常见的依赖方式有:
- 基于rust官方仓库crates.io,通过版本说明来描述;
- 基于项目源代码的git仓库地址,通过URL来描述;
- 基于本地项目的绝对路径或者相对路径,通过类Unix模式的路径来描述。
[dependencies]
typemap = "0.3" #基于rust官方仓库crates.io
plugin = "0.2*" #基于rust官方仓库crates.io
ff-zeroize = { version = "0.6.3", features = [“derive”]} #基于rust官方仓库crates.io
color = { git = "https://github.com/bjz/color-rs" } #基于项目源代码的git仓库地址
geometry = { path = "crates/geometry" } #基于本地项目的绝对路径或者相对路径
若嫌默认源下载速度慢,可切换crates.io为国内镜像源,方法为vim ~/.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"
还因各种原因,即使切换为国内镜像源,仍然无法从crates.io中下载各依赖包的情况。 接下来,了解下Cargo.toml中从crates.io下载的具体工作原理。
2. Cargo如何下载依赖在Cargo中,存在名为registry的概念,默认的registry是http://crates.io。每个registry对应一个指向Git repository的URL,http://crates.io对应的Git repository是https://github.com/rust-lang/crates.io-index。这个GIt repository是一个特定组织形式的文件树,其中根目录下存在一个名为config.json的文件,其中指定了文件下载URL和API查询URL。
以国内中科大镜像源为例,网址为:http://mirrors.ustc.edu.cn/crates.io-index/ 其中config.json内容为:
{
"dl": "https://crates-io.proxy.ustclug.org/api/v1/crates",
"api": "https://crates.io/"
}
默认的源为:https://github.com/rust-lang/crates.io-index/ 其中config.json内容为:
{
"dl": "https://crates.io/api/v1/crates",
"api": "https://crates.io"
}
经验证,无法下载是因为局域网内https://crates-io.proxy.ustclug.org/api/v1/crates被屏蔽无法连接,而https://crates.io/api/v1/crates 可连接成功。
最简单的方法就是切换为默认源,哪怕慢点。
~/.cargo/registry
中存在cache、index、src三个文件夹,具体含义为: 1)index文件夹 Cargo会在本地保存一个index的bare repository作为缓存,用于本地计算依赖列表。每次需要生成或更新项目Cargo.lock文件时,都会利用Git的增量更新功能保证本地index处于最新状态。该index文件夹默认位于~/.cargo/registry/index下,分registry存放。 完成Cargo.lock的生成后,将会根据index中config.json内名为”dl”字段对应的URL获得每个crate依赖的下载路径。如默认index中dl对应的值为“https://crates.io/api/v1/crates“, 以
ff-zeroize = { version = "0.6.3", features = [“derive”]}
为例,则对应的下载路径为“https://crates.io/api/v1/crates/ff-zeroize/0.6.3/download“下载。(格式为“https://crates.io/api/v1/crates/{crate名}/{版本号}/download“) 2)cache文件夹 通过上述下载链接下载的
{crate名}-{版本号}.crate
文件存储在~/.cargo/registry/cache
目录下,根据registry分文件夹存放。【若受限于空间,可定时清空。】 3)src文件夹 这些扩展名为crate的文件事实上是gzip文件,这些crate解压后的内容将存放于
~/.cargo/registry/src
目录下,根据registry分文件夹存放。
参考资料: [1] https://wiki.jikexueyuan.com/project/rust-primer/cargo-projects-manager/cargo-projects-manager.html [2] https://zhuanlan.zhihu.com/p/64917441