module-concept

Rust Packages, crate roots and library/binary targets

A Cargo package is described by a manifest and can contain multiple compilation targets. In this package, lib.rs is the library crate root and main.rs is the binary crate root. Each has its own module tree and crate:: root. The binary accesses the library via its public API. The default library crate name replaces hyphens in the package name with underscores. Multi-file fixture: package-targets. The code shown is one source file, not a standalone program. Expected result (not executed): 12 Why: Cargo builds the library and makes it available to the binary. Common mistake: Treating crate:: inside main.rs as the root of lib.rs.

Formal Syntax & Structure

rust syntaxrust
src/lib.rs; src/main.rs; Cargo.toml

Working Example

Example Snippetrust
fn main() { println!("{}", receipt_demo::total(3)); }
Rust Reference