Cargo packages and dependencies
Cargo packages and dependencies
A project needs more than a source file when it has several modules, tests and external libraries. Cargo provides a standard package workflow. A manifest describes the project and dependencies; a lockfile records the versions Cargo resolved for repeatable builds.
What you’ll learn
- Choose Cargo check, run, test or release build for the task.
- Distinguish a manifest, lockfile and dependency review.
Cargo packages and dependencies in action
Manifest describes package
Place the code in the school package’s src/main.rs and run cargo run.
Manifest describes packageCargo checks and builds
It needs only the standard library, so no dependency entry is required.
Cargo checks and buildsExecutable prints two lines
The two displayed lines are program output; Cargo build diagnostics are separate.
Executable prints two lines
Cargo packages and dependencies explained
Use cargo check for type checking without producing the final executable, cargo run to build and run, and cargo build --release for the release profile. Cargo.toml specifies edition = "2024" for these lessons. Adding a crate means reviewing its API, version requirements and environment, not merely pasting a name.
Work through the Rust program
Save the complete source as main.rs in the Rust environment. No standard input is needed.
Rust 2024 · complete program
fn main() {
println!("Package: school");
println!("Built with Cargo; no external crate needed");
}Expected output
Package: school
Built with Cargo; no external crate neededFollow the execution
Place the code in the school package’s src/main.rs and run cargo run. It needs only the standard library, so no dependency entry is required. The two displayed lines are program output; Cargo build diagnostics are separate.
Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.
Try a small variation
Your task: Does a successful cargo check mean the program has run and its report policy is correct?
Rust 2024 · complete program
fn main() {
println!("Package: school");
println!("Built with Cargo; no external crate needed");
}Worked answer
No. It checks compilation requirements without running the application. Execute the program and test the intended behavior separately.
Mistakes and limits to recognize
Profiles can change optimization and overflow-check settings. A lockfile does not audit a dependency’s security or guarantee identical operating-system behavior. Workspaces organize multiple packages; they are useful when boundaries justify them, not a requirement for every first program.
Check your understanding
Take the idea into your next program
No. It checks compilation requirements without running the application. Execute the program and test the intended behavior separately. Profiles can change optimization and overflow-check settings.
Further reading: the official Rust Book on cargo packages and dependencies.
Reaching the recap and completing the quizzes are separate milestones.