Sub topic 7
Library and binary crates in one package
This chapter assumes you can write modules, use pub, and run cargo build and cargo run. It builds on the idea that a package is what Cargo manages and a crate is what the compiler builds.
One package, several crates
A package can hold one library crate and any number of binary crates. Cargo finds them by file location, so Cargo.toml needs no extra configuration.
| File | Crate type | Crate name |
|---|---|---|
| `src/lib.rs` | Library | Package name |
| `src/main.rs` | Binary | Package name |
| `src/bin/tool.rs` | Binary | `tool` |
Each file is the root of its own module tree. The library and the binary are separate crates, even though they share a name.
Using the library from the binary
Put the reusable code in the library:
// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}The binary imports it by the package name, like any external crate:
// src/main.rs
use my_geometry::add;
fn main() {
println!("{}", add(2, 3));
}Two details matter here:
- The binary sees only
pubitems from the library. A private function inlib.rsis invisible tomain.rs. - A hyphen in the package name becomes an underscore in code. A package named
my-geometryis imported asmy_geometry.
Inside main.rs, crate:: refers to the binary crate, not the library. Library items always need the my_geometry:: prefix.
Extra binaries
Each file in src/bin/ becomes its own binary. Run one with cargo run --bin tool. If a package has several binaries, cargo run without --bin fails with "could not determine which binary to run". Choose a default in the manifest:
[package]
name = "my_geometry"
version = "0.1.0"
edition = "2021"
default-run = "my_geometry"Keep the binary thin
Put the logic in the library and keep main.rs small. It should parse arguments, call library functions, and report errors. This split has a practical payoff for testing:
- Integration tests in
tests/can import the library crate. They cannot import a binary crate. - Doc tests run only for library crates.
- Anything in
main.rsis hard to test without running the whole program.
The later chapters on integration tests and doc tests depend on this layout.
Common errors
unresolved import my_geometry. The package has nosrc/lib.rs, or the name is misspelled. A package with onlymain.rshas no library to import.mod lib;inmain.rs. This compileslib.rsa second time as a module of the binary. You then have two unrelated copies of the code, and the types in them are not compatible. Import the library by name instead.function is private. The item inlib.rsis missingpub. Binaries use the library's public API, the same as any other crate.
Exercise
A package named route-planner has src/lib.rs with pub fn plan() {} and src/main.rs. How does main.rs call plan, and why can't it write crate::plan()?
Show answer
It writes use route_planner::plan; and calls plan(), or calls route_planner::plan() directly. The hyphen becomes an underscore. crate:: in main.rs refers to the binary crate's own root, which does not contain plan. The function lives in the separate library crate.