wiki/content/20200918180750-cargo.md

2.3 KiB

date id title
20200918 c409c0cd-5284-4333-ae99-bc351ff8ba0d Cargo

Description

Cargo is Rust's build system and package manager.

Configuration

Commands

Create project

cargo new hello_cargo

Build & run project

cargo run

Backtrace

When you want to see an error backtrace set the RUST_BACKTRACE environment variable:

RUST_BACKTRACE=1 cargo run

Publish to Crates.io

cargo publish

Install package

cargo install ripgrep

Linting & testing

Check code

cargo check

Testing

cargo test
  1. Backtrace

    To backtrace set the RUST_BACKTRACE environment variable:

    RUST_BACKTRACE=1 cargo run
    
  2. Threads

    By default cargo runs test in parallel. For more control over this you can pass the number of threads you want to use. For example to only use 1 thread:

    cargo test -- --test=threads=1
    
  3. Show output for passing tests as well as failed tests

    cargo test -- --show-output
    
  4. Pass test name to cargo (this equals test function name)

    cargo test one_hundred
    
  5. Run ignored tests

    cargo test -- --ignored
    

Fix

The rustfix tool is included with Rust installations and can automatically fix some compiler warnings.

cargo fix

Builds

Profiles

In Rust, release profiles are predefined and customizable profiles with different configurations that allow a programmer to have more control over various options for compiling code. Each profile is configured independently of the others.

Cargo has two main profiles: the dev profile Cargo uses when you run cargo build and the release profile Cargo uses when you run cargo build --release. The dev profile is defined with good defaults for development, and the release profile has good defaults for release builds.

  1. dev

    cargo build
    
  2. build

    cargo build --release
    

Documentation

See Rust Comments for documentation syntax.

Generation

cargo doc

Open in browser

cargo doc --open