- Cargo is Rust’s build system and package manager.
- Cargo handles the following -
- Building code.
- Downloading libraries.
- Build libraries/dependencies.
- You can use Cargo to create packages. Doing so does the following -
Cargo.toml
file - Cargo configuration file.
src
directory - contains main.rs
.
- Initializes a git repository with a
.gitignore
file.
- Packages of code are called crates.
- Building a Cargo project gives a
Cargo.lock
file.
- This is important for reproducible builds.
- Sets the versions on the initial
cargo build
run.
- Versions are not changed until an explicit update is called.
Cargo Commands
- Create a new project -
cargo new
- Build -
cargo build
- By default, build an executable with debug info.
- Use
cargo build --release
for optimized binary.
- Note, this would increase compile time.
- Run -
cargo run
- Compilation check -
cargo check
- Update crate -
cargo update
- Format code -
cargo fmt