RustBrock/Crates.md

35 lines
1.4 KiB
Markdown

# Crates
The smallest amount of code that the rust compiler will consider
This includes single binary exe files compiled with ``rustc``
``cargo build`` also counts and so does ``cargo run`` (implicit ``cargo build``)
Crates can contain modules and modules may be defined in other files that get compiled with the crate
It comes in two forms a library or a binary
## Binary Crates
are programs that can be compiled then run like a cli application or a server
each must have main function that defines what happens when the executable runs
## Library Crates
are programs that don't have a main function and don't compile to a executable
instead it defines functionality that is intended to be shared with multiple projects
most of the time when the term "crate" is said it means a library crate
and crate is used interchangeably with the term library with other languages
these can be created by ``cargo new crate_name --lib``
## Root Crate
this is the source file that the compiler starts from and makes up the root module of your crate
this is normally src/main.rs in a binary crate
scr/lib.rs is normally the root in a library crate with the same name as the package
hence why it doesn't need to be mentioned in the toml file this tells you what the packages are
cargo passes root crates to rustc to build the library or the binary
by replacing files in the src/bin directory each file will be its own binary crate