cargo is rust's build system, package manager, test-runner, and lots more.
it's sort of rust's swiss army knife.
Documentation
Global Locations
~/.cargo/bin/* |
cargo installed executables
|
~/.config/registry/* |
cached cargo registry indexes
|
~/.cargo/config.toml |
global config
|
~/.config/credentials.toml |
cargo registry credentials
|
Project Locations
${PROJECT}/Cargo.toml |
project config
|
${PROJECT}/Cargo.lock |
frozen package versions
|
Usage
cargo new my_project # create project
# build
cargo build # compile project (for testing)
cargo run # compile/run executable
cargo check # check project can build, without building
cargo build --release # build w/ optimizations (more performant)
cargo test # run tests
# dependencies
cargo update # update all packages
cargo update -p regex # update only 'regex' package
Project Configuration
Basics
# ${PROJECT}/Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]
time = "0.1.12"
regex = "0.1.41"
rand = { git = "https://github.com/rust-lang-nursery/rand.git", rev = "9f35b8e" }
Dependencies
- To add a dependency, add it to your
Cargo.toml
under [dependencies]
.
- Then run
cargo build
echo 'rand = "0.8.5"' > Cargo.toml
cargo build
# ${PROJECT}/Cargo.toml
[dependencies]
time = "0.1.12"
foo = "^0.8.5" # 0.8.5 <= 0.9
rand = { git = "https://github.com/rust-lang-nursery/rand.git", rev = "9f35b8e" }
gif = { version = "0.11.1", optional = true }
- Optional dependencies define features of the same name
Features
Features can be defined, and enabled/disabled at build time.
Define features in your Cargo.toml
# ${PROJECT}/Cargo.toml
[features]
lua = []
python = []
airline = ["python"]
#[cfg(feature = "python")]
pub mod embed_python;
Workspaces