TExcelTAD
Home › Cargo in Depth › Packages, Crates & Targets

Packages, Crates & Targets

Three words get mixed up constantly in Rust: package, crate and target. Cargo's behaviour only makes sense once they are separate in your head.

Term What it is Example
Crate one compilation unit: what one rustc invocation turns into a library or a binary the serde library; your main.rs program
Package a folder with a Cargo.toml: a name, a version and one or more targets hello with Cargo.toml + src/
Target one crate a package builds: at most one library, any number of binaries, examples, tests and benches src/lib.rs, src/main.rs, src/bin/admin.rs

On crates.io people say "crate" for the whole package; Cargo's own messages say package. Both are fine as long as you know which one you mean.

A library and a binary in one package

cargo new --lib creates a library; cargo new (or --bin) creates a binary. A package can have both: put the shared logic in src/lib.rs and keep src/main.rs thin. The binary uses the library through the package name like any external crate:

bash · try it yourself
cargo new temp --vcs none -q
cd temp
cat > src/lib.rs <<'RS'
/// Converts Celsius to Fahrenheit.
pub fn to_fahrenheit(c: f64) -> f64 {
    c * 9.0 / 5.0 + 32.0
}
RS
cat > src/main.rs <<'RS'
fn main() {
    for c in [-40.0, 0.0, 37.0] {
        println!("{c} C = {} F", temp::to_fahrenheit(c));
    }
}
RS
cargo run
   Compiling temp v0.1.0 (/home/learner/temp)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/temp`
-40 C = -40 F
0 C = 32 F
37 C = 98.6 F

Only one Compiling line, but Cargo built two crates: the library temp and the binary temp, which links to it. That split is what lets integration tests, examples and other packages reuse your code: they can all link to a library, but never to a binary.

Several binaries: src/bin/

Every .rs file in src/bin/ (or folder with a main.rs in it) is another binary target, discovered automatically. With more than one binary, cargo run needs to know which one:

bash · try it yourself
cargo new tools --vcs none -q
cd tools
mkdir -p src/bin/report
echo 'fn main() { println!("main tool"); }' > src/main.rs
echo 'fn main() { println!("admin tool"); }' > src/bin/admin.rs
echo 'fn main() { println!("report tool"); }' > src/bin/report/main.rs
cargo run -q 2>&1
cargo run -q --bin admin
cargo run -q --bin report
cargo build -q
ls target/debug | grep -v '\.d
  

 | grep -v -e deps -e build -e examples -e incremental
error: `cargo run` could not determine which binary to run. Use the `--bin` option to specify a binary, or the `default-run` manifest key.
available binaries: admin, report, tools
admin tool
report tool
admin
report
tools

Set default-run = "tools" under [package] if one binary should be the default. Auto-discovery covers src/lib.rs, src/main.rs, src/bin/, examples/, tests/ and benches/; the Targets lesson shows how to configure targets explicitly with [lib], [[bin]] and friends.

cargo init: adopt an existing folder

cargo new creates a new folder. cargo init turns the current folder into a package and detects existing sources, even when they don't follow the conventional layout:

bash · try it yourself
mkdir legacy && cd legacy
echo 'fn main() { println!("written before Cargo"); }' > main.rs
cargo init --vcs none
cat Cargo.toml
cargo run -q
    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package]
name = "legacy"
version = "0.1.0"
edition = "2024"

[dependencies]

[[bin]]
name = "legacy"
path = "main.rs"
written before Cargo

Because main.rs is not under src/, cargo init wrote an explicit [[bin]] table pointing at it. Once a target is listed explicitly, auto-discovery of that kind of target still works for the others.

What a crate name looks like to Rust

Package names may contain -, Rust identifiers may not. Cargo converts the library name by replacing - with _: a package my-utils is used as my_utils::... in code. Binary names keep the hyphen (target/debug/my-utils).

Note: a package can contain at most one library. If you need two, you need two packages, usually in one workspace.

Test yourself

A package has src/lib.rs, src/main.rs and src/bin/admin.rs. How many crates does cargo build compile for it?

  1. 1
  2. 2
  3. 3
  4. It fails: a package can't have both a lib and bins

With several binaries and no default-run, what does cargo run (no flags) do?

  1. Runs the first binary alphabetically
  2. Fails and lists the available binaries; pass --bin <name>
  3. Runs all of them
  4. Runs src/main.rs

How does src/main.rs refer to the library of package my-utils?

  1. crate::
  2. my-utils::
  3. my_utils::
  4. lib::