Ben Chuanlong Du's Blog

It is never too late to learn.

Tips on Cargo

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Create a Project

cargo init
cargo new project_name
cargo new --lib project_name

Install a Rust Crate (Package)

Install a Rust crate from GitHub (the default branch).

cargo install --git https://github com/RustPython/RustPython

Specify the option --version to install a specific version of a package.

cargo install --version 0.8.1 evcxr_jupyter

Update Dependencies

Please refer to cargo-update for the official tutorial.

Build a Project

Build the debug version of the project.

cargo build

Build the release version of the project.

cargo build --release

https://doc.rust-lang.org/cargo/commands/cargo-build.html

Run The Current Package

cargo run
cargo run --release

For more details, please refer to cargo-run .

Run Unit Tests

Please refer to Unit Test in Rust for more discussions.

Running Test

Controlling How Tests Are Run

cargo test -- --test-threads=1

cargo test -- --show-output

cargo test -- --ignored

cargo test -- --ignored --show-output

cargo test --release

cargo test --release -- --ignored

Suppress Warnings Using RUSTFLAGS

Before cargo officially supports an option to disable warnings, you can use the environment variable RUSTFLAGS to disable warnings during compiling.

RUSTFLAGS=-Awarnings cargo build
RUSTFLAGS=-Awarnings cargo build --release
RUSTFLAGS=-Awarnings cargo test
RUSTFLAGS=-Awarnings cargo test --release

A better way is to use the command cargo rustc (instead of cargo build) which allows users to pass compiler flags to it.

cargo rustc --lib -- -Awarnings

Cargo Extensions / Addons

Please refer to Dev Tools for Rust for detailed discussions.

References

Comments