पाठ 3 / 25

Crates, Cargo.toml and Modules

Packages and code organisation.

Crates, editions and modules

A crate is a compilation unit: a binary (with main.rs) or a library (lib.rs). Cargo.toml names the package, sets the edition (2015, 2018, 2021, 2024: opt-in language updates that keep old code compiling), and lists dependencies from crates.io with version requirements; Cargo.lock records exact versions. Code is organised into modules (mod orders; maps to orders.rs or orders/mod.rs); items are private by default and exported with pub.

A Cargo.toml with dependencies

Typical manifest; versions are examples.

[package]
name = "shop"
version = "0.1.0"
edition = "2024"

[dependencies]
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }

[dev-dependencies]
pretty_assertions = "1"

Commit Cargo.lock for applications

For binaries, committing Cargo.lock makes builds reproducible; libraries usually leave it to their users.

त्वरित जाँच: How do you make a function visible outside its module?

  • Use the export keyword
  • Name it with a capital letter
  • Put it in Cargo.toml
  • Mark it pub
Answer

Mark it pub — Private by default.