Cargo
Cargo is Rustβs built-in package manager and build system. It also supports the following actions,
| Command | Action |
|---|---|
cargo new | Create a new project |
cargo init | Create a new project in an existing directory |
cargo check | Verify the project compiles without errors |
cargo build | Build the executable |
cargo run | Build the executable and run |
cargo clean | Remove the build system directories/ target directory |
π‘ The
cargo checkcommand verifies that the project compiles without errors, without producing an executable. Thus, it is often faster thancargo build.
π‘ Cargo places executables compiled with
cargo buildorcargo runin thetarget/debug/directory. But, while those built withcargo build --releasefor release purposes are stored intarget/release/directory. Release builds use more optimizations and remove some runtime safety checks to increase performance, although this comes at the cost of longer compile time.
| Command | Action |
|---|---|
cargo add | Add a dependency crate to the project |
cargo remove | Remove a dependency crate from the project |
cargo fetch | Download the dependencies specified in Cargo.lock |
cargo update | Update project dependencies |
π‘ A crate is a package that can be shared via crates.io, Rust communityβs crate registry.
cargo add,cargo remove,cargo fetch, andcargo updatecommands manage project dependencies through the crate hosted on crates.io.
π‘ The
cargo addcommand includes a specified crate in the[dependencies]section ofCargo.toml, whilecargo add --devadds a crate to the[dev-dependencies]section. This indicates that the crate is only used for development purposes like testing and will not be included in the final compiled code.
| Command | Action |
|---|---|
cargo test | Run tests |
cargo bench | Run benchmarks |
cargo doc | Generate the project documentation via rustdoc |
In addition, there are cargo commands to publish the project as a crate to crates.io.
| Command | Action |
|---|---|
cargo login | Login to crates.io with the API token |
cargo package | Make the local crate uploadable to crates.io |
cargo publish | Upload the crate to crates.io |
cargo install | Install a Rust binary |
cargo uninstall | Uninstall a Rust binary |
π‘ You need to get an API token from crates.io to publish a crate to it. The API token can be found in the Account Settings page, after login to that site. We will discuss more about this under code organization with crates.
Crate
A crate is a package, which can be shared via Rust communityβs crate registry, crates.io.
A crate can produce an executable or a library. In other words, it can be a binary crate or a library crate.
cargo new crate_name --binorcargo new crate_name: Produces an executablecargo new crate_name --lib: Produces a library
The first one generates,
βββ Cargo.toml
βββ src
βββ main.rs
and the second one generates,
βββ Cargo.toml
βββ src
βββ lib.rs
- Cargo.toml(capital c) is the configuration file which contains all of the metadata that Cargo needs to compile your project.
- src folder is the place to store the source code.
- Each crate has an implicit crate root/ entry point. main.rs is the crate root for a binary crate and lib.rs is the crate root for a library crate.
Project Structure
This is how Cargo documentation describes about the recommended project layout,
.
βββ Cargo.toml
βββ Cargo.lock
βββ src
β βββ main.rs
β βββ lib.rs
β βββ bin
β βββ another_executable.rs
β βββ multi_file_executable
β βββ main.rs
β βββ some_module.rs
βββ tests
β βββ some_integration_tests.rs
βββ benches
β βββ simple_bench.rs
βββ examples
βββ simple_example.rs
- The source code goes in the
srcdirectory.- The default executable file is
src/main.rs. - The default library file is
src/lib.rs. - Other executables can be placed in,
src/bin/*.rssrc/bin/*/main.rs
- The default executable file is
- Integration tests go in the
testsdirectory (unit tests go in each file they’re testing). - Benchmarks go in the
benchesdirectory. - Examples go in the
examplesdirectory.
Rust Editions
Rust guarantees backward compatibility while introducing major updates to the language. To support this, the edition field was added to the Cargo.toml file in Rust 2018, marking the first major update to the language ecosystem three years after its initial release. Editions are opt-in, meaning existing crates will not experience these changes until they explicitly migrate to the new edition.
The major editions of Rust are:
Rust 2015: The initial edition, introduced with Rust 1.0. It established the core language features like ownership, borrowing, and lifetimes, laying the foundation for Rustβs safety and concurrency guarantees.
Rust 2018: The first major update, introduced the
editionfield inCargo.toml, simplified the module system, stabilizedasync/await, improved error handling with the?operator, and made several syntactic changes.Rust 2021: Focused on improving ergonomics and removing inconsistencies, such as disjoint closure capture,
IntoIteratorfor arrays, and the introduction of or-patterns in macros.Rust 2024: The latest edition, includes enhancements like refined
asyncfeatures, moreconstgenerics, better diagnostics, and improved Cargo features.
For new projects created by cargo new, it will set edition = "2024" by default in the Cargo.toml file. For example,
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024"
π¨βπ« Before going to the next…
The
.cargo/bindirectory of your home directory is the default location of Rust binaries. Not only the official binaries likerustc,cargo,rustup,rustfmt,rustdoc,rust-analyzerand also the binaries you can install viacargo installcommand, will be stored in this directory.Even though the initial convention for naming crates and file names is using the
snake_case, some crate developers are usingkebab-caseon both crates and file names. To make your code more consistent, use the initial conventionsnake_case; especially on file names.Create,
an executable crate via
cargo newcommand, run it viacargo runand examine the files and project structure.a library crate via
cargo newcommand, runcargo testand examine the files and project structure.a multiple executables project and try to run each executable.
- You can name executables in the
Cargo.tomlfile.[[bin]] name = "app" path = "src/bin/app/main.rs" - You can use the
--binflag to specify the executable, while runningcargocommands.
Ex.cargo build --bin app,cargo run --bin app - You can set default executable in the
Cargo.tomlfile.[package] name = "hello_world" version = "0.1.0" edition = "2024" default-run = "app"
- You can name executables in the
Run
cargo build --releaseand check the files in thetargetfolder.