Dependencies
crates.io is the Rust community's central package registry that serves as alocation to discover and download packages. cargo
is configured to use it bydefault to find requested packages.
To depend on a library hosted on crates.io, add it to your Cargo.toml
.
Adding a dependency
If your Cargo.toml
doesn't already have a [dependencies]
section, add that,then list the crate name and version that you would like to use. This exampleadds a dependency of the time
crate:
[dependencies]
time = "0.1.12"
The version string is a semver version requirement. The specifyingdependencies docs have more information aboutthe options you have here.
If we also wanted to add a dependency on the regex
crate, we would not needto add [dependencies]
for each crate listed. Here's what your wholeCargo.toml
file would look like with dependencies on the time
and regex
crates:
[package]
name = "hello_world"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]
time = "0.1.12"
regex = "0.1.41"
Re-run cargo build
, and Cargo will fetch the new dependencies and all oftheir dependencies, compile them all, and update the Cargo.lock
:
$ cargo build
Updating crates.io index
Downloading memchr v0.1.5
Downloading libc v0.1.10
Downloading regex-syntax v0.2.1
Downloading memchr v0.1.5
Downloading aho-corasick v0.3.0
Downloading regex v0.1.41
Compiling memchr v0.1.5
Compiling libc v0.1.10
Compiling regex-syntax v0.2.1
Compiling memchr v0.1.5
Compiling aho-corasick v0.3.0
Compiling regex v0.1.41
Compiling hello_world v0.1.0 (file:///path/to/package/hello_world)
Our Cargo.lock
contains the exact information about which revision of all ofthese dependencies we used.
Now, if regex
gets updated, we will still build with the same revision untilwe choose to cargo update
.
You can now use the regex
library in main.rs
.
use regex::Regex;
fn main() {
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
println!("Did our date match? {}", re.is_match("2014-01-01"));
}
Running it will show:
$ cargo run
Running `target/hello_world`
Did our date match? true