</>

Rust Cheat Sheet

Practical Rust cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.

rust syntax examples reference

Rust cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the Regex Cheat Sheet and Ruby Cheat Sheet.

Setup and Tooling

Goal: Install and verify language toolchain

# Run rustc command
rustc --version

# Run cargo command
cargo --version

# Run cargo command
cargo new hello-rust

Core Workflows

Goal: Run and build source code

# Execute cd workflow
cd hello-rust && cargo run

# Execute cargo workflow
cargo check

Goal: Use a practical code snippet

// Borrowed slice processing without allocations
fn count_even(values: &[i32]) -> usize {
    values.iter().filter(|v| **v % 2 == 0).count()
}

fn main() {
    println!("{}", count_even(&[1, 2, 3, 4]));
}

Testing and Formatting

Goal: Run automated tests

# Execute test-related command
cargo test

# Execute test-related command
cargo test -- --nocapture

Goal: Run lint and formatter checks

# Execute quality command
cargo fmt

# Execute quality command
cargo clippy --all-targets --all-features

Automation and CI

Goal: Create a repeatable CI shell step

# Run strict shell mode for CI step
set -euo pipefail

# Install dependencies deterministically
npm ci || pip install -r requirements.txt || true

# Run tests and fail fast on errors
cargo test

Debugging and Troubleshooting

Goal: Trace runtime issues

# Print runtime and package manager versions
node --version || python --version || true

# Search stack traces and TODO markers
rg "Traceback|Exception|TODO" .

# Re-run tests with verbose output
cargo test -v || cargo test --verbose || true

Common Gotchas

  • Pin Rust language/runtime versions in local and CI environments.
  • Keep formatting and linting automated to avoid noisy diffs in code review.
  • Prefer explicit dependency lock files for reproducible builds.
  • Write small, deterministic tests that can run quickly in pre-commit checks.
  • Document compiler/interpreter flags used in production builds.

Related Cheat Sheets