Language cheatsheet
Rust and Cargo 1 Cheatsheet
Cargo and rustup commands for projects, dependencies, testing and releases, with the Rust patterns you reach for most.
Projects and toolchain
- Check the Rust version — Print the installed compiler version.
- Update Rust — Update the installed toolchains to their latest releases.
- Create a new binary project — Create a project with a main.rs and a Cargo.toml.
- Create a new library — Create a library crate with a lib.rs.
- Initialize Cargo in the current folder — Turn an existing folder into a Cargo package.
- Add a compilation target — Install the standard library for another target, here WebAssembly.
- Install clippy and rustfmt — Add the linter and formatter to the active toolchain.
- Install the nightly toolchain — Install a toolchain for unstable features, then run with cargo +nightly.
Build and run
- Type-check quickly — Check that the code compiles without producing a binary. Much faster than a build.
- Build in debug mode — Compile the project with debug info and no optimization.
- Build for release — Compile with optimizations into target/release.
- Run the project — Build and run the binary.
- Run with arguments — Pass arguments to your program after the double dash.
- Show a backtrace on panic — Set RUST_BACKTRACE to see where a panic came from.
- Delete build output — Remove the target folder to free disk space or force a full rebuild.
- Build and open documentation — Build the docs for your crate and its dependencies and open them in a browser.
Dependencies
- Add a dependency — Add a crate to Cargo.toml, here serde with its derive macros.
- Add a development dependency — Add a crate used only for tests and examples.
- Remove a dependency — Remove a crate from Cargo.toml.
- Update dependencies — Update Cargo.lock to the newest versions Cargo.toml allows.
- Show the dependency tree — Print the dependencies as a tree.
- Find duplicate crate versions — List crates that appear in more than one version.
- Install a command-line tool — Build and install a crate's binary into ~/.cargo/bin.
- Check for vulnerable dependencies — Check Cargo.lock against the RustSec advisory database. Needs cargo-audit installed.
Testing and code quality
- Run the tests — Run unit tests, integration tests and doc tests.
- Run matching tests — Run tests whose names contain the text.
- Show test output — Show what tests print. Output is hidden for passing tests by default.
- Run benchmarks — Run benchmarks. The built-in harness needs the nightly toolchain, so most projects use criterion.
- Format the code — Format every file with rustfmt.
- Check formatting in CI — Fail if any file isn't formatted, without changing files.
- Lint the code — Run clippy and treat warnings as errors, as many CI pipelines do.
- Apply compiler suggestions — Automatically apply fixes the compiler suggests.
- Check a crate before publishing — Package and verify a crate without uploading it.
Code patterns
- Hello world — The smallest runnable Rust program.
- Define a struct — Define a type and derive common traits for debugging, copying and comparing.
- Propagate errors with ? — Return early with the error when a call fails.
- Match on a value — Handle every case of an enum or option.
- Transform a collection — Chain iterator adapters, then collect the result.
- Async main with Tokio — Run async code with the Tokio runtime. Add it with cargo add tokio --features full.
- Use a HashMap — Insert values and read them back.
Related cheatsheets
- C++ 23 cheatsheet — Compile and debug C++ with g++ and CMake, and use modern C++ (up to C++23): containers, algorithms, smart pointers, templates and threads.
- C# 14 cheatsheet — Modern C# 14 syntax for strings, nullability, collections, LINQ, records, pattern matching, async code and generics.
- CSS 3 cheatsheet — Layout with flexbox and grid, responsive design, custom properties, animations and modern selectors.
- Go 1 cheatsheet — The go command for modules, building, testing and cross-compiling, plus the code patterns you write every day.
- HTML 5 cheatsheet — HTML5 page structure, text, links, images, forms, tables and metadata, with accessible patterns.
- Java, Maven and Gradle 25 cheatsheet — JDK commands to compile, run and diagnose Java programs, plus the Maven and Gradle commands for builds and tests.