Cargo helps you add and manage external code libraries (called dependencies) easily in your Rust projects.
Cargo dependency management in Rust
[dependencies] crate_name = "version" # Example: serde = "1.0"
Dependencies are listed in the Cargo.toml file under the [dependencies] section.
The version uses quotes and usually follows semantic versioning like "1.0" or "^1.0".
rand crate version 0.8 as a dependency.[dependencies]
rand = "0.8"serde with the derive feature enabled.[dependencies]
serde = { version = "1.0", features = ["derive"] }regex version compatible with 1.5 (like 1.5.3, 1.6, etc.).[dependencies]
regex = "^1.5"This program uses the rand crate to generate a random number between 1 and 10. The dependency is declared in Cargo.toml under [dependencies]. Cargo downloads and manages the rand crate automatically.
[package] name = "hello_cargo" version = "0.1.0" edition = "2021" [dependencies] rand = "0.8" // src/main.rs use rand::Rng; fn main() { let mut rng = rand::thread_rng(); let n: u8 = rng.gen_range(1..=10); println!("Random number between 1 and 10: {}", n); }
Run cargo build or cargo run to let Cargo download and compile dependencies.
You can update dependencies with cargo update.
Use exact versions or version ranges to control updates.
Cargo manages external libraries by listing them in Cargo.toml.
Dependencies help you reuse code and add features easily.
Cargo downloads, compiles, and updates dependencies automatically.
Practice
Cargo.toml file in a Rust project?Solution
Step 1: Understand Cargo.toml role
TheCargo.tomlfile is used to declare dependencies and project metadata.Step 2: Differentiate from other files
The Rust code logic is insrc/main.rs, notCargo.toml. Compilation is done by Cargo commands, not by the file itself.Final Answer:
To list and manage the project's dependencies -> Option DQuick Check:
Cargo.toml manages dependencies [OK]
- Confusing Cargo.toml with source code files
- Thinking Cargo.toml compiles code
- Assuming Cargo.toml stores executables
serde version 1.0 in Cargo.toml?Solution
Step 1: Recall Cargo.toml dependency syntax
Dependencies are listed asname = "version"pairs in Cargo.toml.Step 2: Match correct syntax
serde = "1.0" matches the correct syntax:serde = "1.0". Other options use invalid symbols.Final Answer:
serde = "1.0" -> Option AQuick Check:
Dependency syntax = name = "version" [OK]
- Using colon or arrow instead of equals
- Omitting quotes around version
- Putting quotes around dependency name incorrectly
Cargo.toml snippet:[dependencies] regex = "1.5"
What happens when you run
cargo build for the first time?Solution
Step 1: Understand Cargo build behavior
When building, Cargo checks dependencies listed in Cargo.toml and downloads missing ones.Step 2: Apply to regex dependency
Since regex 1.5 is listed, Cargo downloads and compiles it automatically on first build.Final Answer:
Cargo downloads and compiles the regex crate version 1.5 -> Option CQuick Check:
cargo build downloads dependencies [OK]
- Thinking dependencies must be installed manually
- Assuming cargo build ignores dependencies
- Expecting errors if dependencies are new
Cargo.toml but get an error: package not found when running cargo build. What is the likely cause?Solution
Step 1: Analyze error meaning
"Package not found" means Cargo cannot find the named crate in the registry.Step 2: Identify common causes
This usually happens if the dependency name is misspelled or the crate does not exist on crates.io.Final Answer:
The dependency name is misspelled or does not exist on crates.io -> Option AQuick Check:
Package not found = wrong dependency name [OK]
- Thinking cargo update fixes missing package errors
- Renaming Cargo.toml to Cargo.lock
- Blaming Rust version without checking dependency name
log in your project: version 0.4 for your code and version 0.3 for a dependency. How can Cargo handle this situation?Solution
Step 1: Understand Cargo's version handling
Cargo supports multiple versions of the same crate in the dependency tree to avoid conflicts.Step 2: Apply to example
Your code can use log 0.4 while a dependency uses log 0.3; Cargo compiles both separately.Final Answer:
Cargo allows multiple versions of the same crate to coexist in the dependency tree -> Option BQuick Check:
Cargo supports multiple crate versions [OK]
- Assuming Cargo fails on version conflicts
- Trying to merge versions manually
- Thinking Cargo ignores older versions
Ready to prove it?
Pass the test and this pattern moves unknown
