What Is the Difference Between a Binary and Library Crate?

Binary crates create executable programs with a main function, while library crates provide reusable code for other projects.

A binary crate produces an executable program, while a library crate produces code for other programs to use. Binary crates define a main function in src/main.rs to serve as the entry point, whereas library crates define reusable functions and types in src/lib.rs.

# Cargo.toml for a binary crate
[package]
name = "my_app"
version = "0.1.0"
edition = "2024"

# Cargo.toml for a library crate
[package]
name = "my_lib"
version = "0.1.0"
edition = "2024"

[lib]
name = "my_lib"
path = "src/lib.rs"
// src/main.rs (Binary)
fn main() {
    println!("Hello, world!");
}

// src/lib.rs (Library)
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}