skip to content

In a Cargo package with src/lib.rs, src/main.rs and two files in src/bin/, which targets does Cargo build, and how do the binaries reach the library?

level: middleimportance: must knowfreq 60%

answer

  1. the directory layout is the configuration
  2. one library, many binaries
  3. binaries link the library like a dependency
  4. dashes become underscores in the crate name
  5. default-run when cargo run cannot choose

basics

~20 s

Cargo discovers one library from src/lib.rs and a binary from src/main.rs plus one per file in src/bin/. Each binary is a separate crate that links the library and calls its public API by crate name, for example geo_tiles::tiles_per_edge.

solid answer

~40 s

Cargo infers targets from the layout. `src/lib.rs` is the package's single library, whose crate name is the package name with dashes turned into underscores (`geo_tiles`). `src/main.rs` is a binary named after the package (`geo-tiles`), and every `src/bin/*.rs` file (or `src/bin/<dir>/main.rs`) is another binary named after the file. `examples/`, `tests/` and `benches/` add example, integration-test and bench targets, which plain `cargo build` does not compile. Each binary is its own crate: it links the library like a dependency and sees only its `pub` items, through paths such as `geo_tiles::tiles_per_edge`. With more than one binary, `cargo run` needs `--bin NAME` or a `default-run` key in `[package]`.

code

rust · 5 lines
rust
// src/bin/tile-seed.rs in the geo-tiles package
fn main() {
    // the library's crate name: dashes in the package name become underscores
    println!("seed {}", geo_tiles::tiles_per_edge(2));
}

go deeper

for a junior

Recall the standard layout: src/lib.rs is the library, src/main.rs and src/bin files are binaries, and a binary reaches the library by its underscore crate name.

for a middle

Explain the naming rules, that each binary is a separate crate linked against the library, and how --bin or default-run resolves an ambiguous cargo run.

for a senior

Use the split deliberately: keep shared logic public in the library so every tool and integration test exercises the same code, and keep binaries as thin entry points.

for a principal

Decide when tools outgrow one package: shared release cadence argues for src/bin, while separate dependencies or owners argue for separate packages.

## Targets, crates and packages A **package** is what one `Cargo.toml` describes. A **target** is one thing Cargo compiles from it, and each target becomes one **crate** when rustc runs. A package can hold at most **one library** target and any number of binary, example, integration-test and bench targets. Most packages never list their targets: Cargo infers them from where the source files sit, a process called **target auto-discovery**. Take a package called `geo-tiles` that ships tile maths as a library plus two operational tools: ```plaintext geo-tiles/ ├── Cargo.toml ├── src/ │ ├── lib.rs │ ├── main.rs │ └── bin/ │ ├── tile-seed.rs │ └── tile-stats.rs ├── examples/render.rs ├── tests/edges.rs └── benches/zoom.rs ``` ## What Cargo discovers | File | Target kind | Target name | Built by plain `cargo build`? | |---|---|---|---| | `src/lib.rs` | library | `geo_tiles` | yes | | `src/main.rs` | binary | `geo-tiles` | yes | | `src/bin/tile-seed.rs` | binary | `tile-seed` | yes | | `src/bin/tile-stats.rs` | binary | `tile-stats` | yes | | `examples/render.rs` | example | `render` | no (`cargo test` compiles examples) | | `tests/edges.rs` | integration test | `edges` | no | | `benches/zoom.rs` | bench | `zoom` | no | Three naming rules produce that table: - The library defaults to the **package name with dashes replaced by underscores**, because a crate name has to be a valid Rust identifier. - The binary from `src/main.rs` takes the **package name as written**, dashes included. - Every other discovered target is named after its **file**, or after its **directory** when a multi-file target uses `src/bin/<dir>/main.rs` (the same works under `examples/`, `tests/` and `benches/`). `cargo build` with no target flags builds the library and all binaries. `cargo test` adds the test targets and compiles examples so they cannot rot, and `cargo build --all-targets` builds everything. ## How a binary uses the library A binary is **not** a module of the library. It is a separate crate, compiled after the library and linked against it the same way an external dependency is. That has two consequences: 1. The binary names the library by its crate name, `geo_tiles::tiles_per_edge(2)`, not with `crate::` (which means the binary's own root) and never with the dashed package name. 2. The binary sees only the library's **public** API. Logic the tools share belongs in the library, behind `pub` items, so each tool stays a thin `main`. How modules and visibility work inside each crate is the Rust language's side of the border; Cargo's part is deciding that these are separate crates and linking them. The same linking rule covers the other target kinds: - **Examples** link the library and the package's `[dev-dependencies]`, so `examples/render.rs` also calls `geo_tiles::...`. - **Integration tests** in `tests/` are separate crates that link the library too. When a package has integration tests, Cargo also builds its binaries and sets `CARGO_BIN_EXE_<name>` (for example `CARGO_BIN_EXE_tile-seed`) so a test can locate and run the executable. - **Benches** in `benches/` link the library the same way; the built-in `#[bench]` harness is unstable, so stable crates usually reach for a benchmarking crate instead. ## Running one binary With a single binary, `cargo run` simply runs it. With three, Cargo cannot guess: ```console $ cargo run 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: geo-tiles, tile-seed, tile-stats ``` Two fixes exist: - pass `--bin tile-seed` (or `--example render` for an example) on each run; - set `default-run = "geo-tiles"` in `[package]`, so a bare `cargo run` picks that binary and `--bin` still selects the others. ## When the layout is not enough Auto-discovery covers the common case. Explicit `[lib]`, `[[bin]]`, `[[example]]`, `[[test]]` and `[[bench]]` tables exist for files outside the standard directories, for renaming a target, or for per-target settings such as `test = false`; since the 2018 edition a hand-written table adds to the discovered targets instead of replacing them. `cargo new --bin` (the default) starts a package with `src/main.rs`, and `cargo new --lib` starts one with `src/lib.rs`; adding the other file later is all it takes to have both.

  • Why does plain cargo build not produce target/debug/examples/render?
    With no target-selection flags, `cargo build` builds only the library and the binaries. Examples are compiled by `cargo test` (to keep them compiling), by `cargo build --example render`, by `cargo run --example render` or by `--all-targets`.
  • How do you give a binary several source files under src/bin?
    Make a directory, `src/bin/tile-seed/main.rs`, and put the extra modules beside `main.rs`. Cargo names the binary after the directory, so it is still `tile-seed`, and the other files are modules of that one binary.
  • Can a package have two library targets?
    No. A package has at most one library, which is why `[lib]` is a single TOML table while `[[bin]]` is an array of tables. A second library means a second package, usually in a workspace.

  • A binary calls library functions through crate:: because they share one package.
  • Each file in src/bin/ becomes a module of src/main.rs.
  • A package may contain several library targets, one per src file.
  • Plain cargo build also compiles examples and integration tests.
  • The library's crate name keeps the dash from the package name.