Sub topic 12
Git, path and renamed dependencies
This chapter assumes you can read a Cargo.toml, run cargo add, and understand SemVer requirements from earlier chapters. Most dependencies come from crates.io, but Cargo can also read a crate from a local folder or a git repository. It can also give a dependency a different name inside your code.
Why use other sources
You need a different source when:
- the crate lives in a folder next to your project and you are editing both together
- you need a fix that is on a git branch but not yet released
- you need two versions of the same crate in one project
Path dependencies
A path dependency points to a folder that contains a Cargo.toml. The path is relative to the manifest that declares it.
[dependencies]
my_local = { path = "../my_local" }Cargo builds the crate from that folder, so edits show up the next time you build. This is useful when you split a project into several crates.
Path dependencies have a limit. If you publish your crate to crates.io, every path dependency must also have a version. Cargo uses the path when you build locally and the version when the crate is published.
[dependencies]
my_local = { path = "../my_local", version = "0.1" }Git dependencies
A git dependency points to a repository URL. Cargo clones it and looks for the crate inside.
[dependencies]
my_fork = { git = "https://github.com/example/my_fork" }By default Cargo uses the default branch. You can pin the source more tightly with one of three keys:
| Key | Selects |
|---|---|
| `branch = "name"` | The latest commit on that branch |
| `tag = "v1.0.0"` | The commit a tag points to |
| `rev = "a1b2c3d"` | A specific commit |
[dependencies]
my_fork = { git = "https://github.com/example/my_fork", tag = "v1.0.0" }Cargo records the exact commit in Cargo.lock. A branch dependency stays on that commit until you run cargo update, which then moves it to the branch's newest commit. A tag or rev does not move.
You can also run cargo add my_fork --git https://github.com/example/my_fork, and cargo add my_local --path ../my_local for a path.
Renamed dependencies
The package key lets you use a name in your code that differs from the crate's real name. The key before = is the name you write in Rust. The package value is the name on crates.io.
[dependencies]
rand07 = { package = "rand", version = "0.7" }
rand08 = { package = "rand", version = "0.8" }Now your code can use both versions side by side:
fn main() {
let old: u8 = rand07::random();
let new: u8 = rand08::random();
println!("{old} {new}");
}Renaming also helps when two crates have names that clash, or when you want a shorter name. The name in your code is rand07, not rand.
Common mistakes
- Using a path that is relative to your shell instead of to the
Cargo.tomlthat contains it. - Expecting a
branchdependency to update on every build. It updates only when you runcargo update. - Forgetting that the renamed dependency's name is the one used in
usepaths and macros. - Publishing with a git or path dependency that has no
version.
Exercise
You write old_rand = { package = "rand", version = "0.7" } in [dependencies]. How do you call the random function in your code, and what must you add to this entry before you publish a crate that uses my_local = { path = "../my_local" }?
Show answer
You call old_rand::random(), because the key is the name used in Rust code. For the path dependency, you must add a version key, for example version = "0.1", so crates.io knows which released version to use.
Exercise
A git dependency uses branch = "main". You build today, and the repository gets new commits tomorrow. What makes your project use the new commits?
Show answer
Running cargo update (for example cargo update -p my_fork). Until then, Cargo.lock keeps the commit it recorded.