How to use lib.rs vs main.rs

Use main.rs for executable programs and lib.rs for reusable library code in Rust projects.

Use src/main.rs for the executable entry point and src/lib.rs for reusable library code. Create a library crate by adding src/lib.rs to your project directory.

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

Use src/main.rs to call library functions or define a standalone binary.

// src/main.rs
use your_crate_name::add;

fn main() {
    println!("Result: {}", add(2, 3));
}