ProgramGuru

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

  1. Manifest describes package

    Place the code in the school package’s src/main.rs and run cargo run.

    Manifest describes package
  2. Cargo checks and builds

    It needs only the standard library, so no dependency entry is required.

    Cargo checks and builds
  3. Executable prints two lines

    The two displayed lines are program output; Cargo build diagnostics are separate.

    Executable prints two lines
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.

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 needed

Follow 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

Does a successful cargo check mean the program has run and its report policy is correct?
Choose one answer. You can try again.

Answer and explanation

No.

No. It checks compilation requirements without running the application. Execute the program and test the intended behavior separately.

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.

    Next lesson: Tests, boundaries and debugging →