Skip to main content

Packages, Crates, and Modules

Crate

The smallest unit that the Rust compiler will work with. When you run rustc some_file.rs the file some_file.rs is treated as a crate file.

A crate when being compiled can be compiled into two forms, a binary crate or a library crate. Binary crate are programs that after being compiled are turned into an executable that you can run. Binary crate must have a function called main that gets called when the executable is ran.

Library crate don't have a main function and they do not get compiled into an executable binary. Instead, they defined functions that are meant to be shared with multiple projects, much like exporting some common functions that you are going to be using in other projects.

Hence in Rust, when you refer to "crates" it is usually library crate, and refer to binary crate as just the executable or binary.

Package

A bundle of one or more crates that provide a set of functionality.

A package contains a Cargo.toml file that tells it how to build those crates. A package can contain as many binary crates as you like, but at most only one library crate. A package must contain at least one crate, whether it is binary or library crate.

 https://www.sheshbabu.com/posts/rust-module-system/