Skip to content

Cargo

Cargo is Rust's package manager.

New crates can be created with the cargo command-line utility.

cargo new hello_cargo
cargo new hello_cargo --vcs none # Prevent creation of a Git repo

Extensions to Cargo are available, which add subcommands like add and watch

cargo install cargo-edit
cargo install cargo-watch

During development, a crate can be compiled and run

cargo run

If behind a corporate firewall, where SSL certificates are substituted, a special flag must be set in ~/.cargo/config.toml to allow package downloads.

[http]
check-revoke = false

Generate documentation from doc comments using a built-in static site generator, and then open it.

cargo doc --open

Other commands:

  • cargo fmt format code
  • cargo test run doctests

Features

Every crate has features that can be enabled in the Cargo.toml. Crate features are declared in the Cargo.toml

rocket = {version = "0.5.0-rc.1", default-features = false, features = ["json"]}

These can also be added from the command-line

cargo add rocket --features json --no-default-features

These are distinct from the "feature flag" inner attributes referring to features of unstable Rust.