Rust
Cargo workflows, simplified
Rust development is elegant. These shortcuts make it even better.
Environment
Rust environment initializes automatically via sh/rust.sh:
# Auto-added to PATH:
~/.cargo/bin # Cargo binaries
~/.rustup/toolchains/*/bin # Toolchain binariesNo manual PATH configuration needed.
Cargo Aliases
| Alias | Command | Description |
|---|---|---|
cr | cargo run | Run the project |
cb | cargo build | Build the project |
ct | cargo test | Run tests |
cf | cargo fmt | Format code |
cc | cargo clippy | Lint code |
cbench | cargo bench | Run benchmarks |
Note: cc clashes with the C compiler on some systems, but Rust context makes it obvious.
Functions
cadd — Add Dependencies
Add crates to your Cargo.toml:
cadd serde # Add serde
cadd serde serde_json # Add multiple
cadd tokio --features full # With featuresRequires cargo-edit:
cargo install cargo-editWay better than manually editing Cargo.toml.
cwatch — Auto-Rebuild
Watch files and rebuild on changes:
cwatch # Watch and rebuild
cwatch test # Watch and run tests
cwatch run # Watch and run
cwatch 'check --all-targets' # Custom commandRequires cargo-watch:
cargo install cargo-watchEssential for development. Instant feedback on every save.
rswitch — Toolchain Switcher
Switch between Rust toolchains interactively:
rswitch
# fzf list of installed toolchains
# Select one to activateUseful when testing compatibility across stable/beta/nightly.
crtree — Dependency Tree
Interactive dependency tree viewer:
crtree
# Shows dependency tree with fzf
# Navigate your deps interactivelyGreat for understanding your dependency graph.
Toolchain Management
Managed by rustup:
# Update all toolchains
rustup update
# Set default toolchain
rustup default stable
rustup default nightly
# Add compilation targets
rustup target add wasm32-unknown-unknown
rustup target add x86_64-pc-windows-gnu
# List installed toolchains
rustup toolchain listWorkflows
New Project
# Create binary project
cargo new myproject
cd myproject
# Or create library
cargo new --lib mylib
cd mylib
# Run it
crDevelopment Loop
# Start watch mode
cwatch
# In another terminal:
# Edit code
vim src/main.rs
# Tests run automatically on save
# Or run manually:
ct
# Check with clippy
ccInstant feedback. Make a change, see the result immediately.
Before Commit
# Format
cf
# Lint
cc
# Test
ct
# If all pass, commit
gigRust's tooling makes this workflow fast.
Adding Dependencies
# Add a crate
cadd reqwest
# With features
cadd tokio --features full,macros
# Dev dependency
cadd --dev proptestMuch faster than manually editing Cargo.toml.
Release Build
# Build optimized binary
cargo build --release
# Binary is in target/release/
ls -lh target/release/myprojectRelease builds are significantly faster than debug builds.
Cross-Compilation
# Add target
rustup target add x86_64-pc-windows-gnu
# Build for target
cargo build --target x86_64-pc-windows-gnu --releaseBenchmarking
# Run benchmarks
cbench
# Specific benchmark
cbench my_bench_functionRequires nightly toolchain for some benchmark features.
Common cargo Commands
# Check without building (fast)
cargo check
# Build docs
cargo doc --open
# Clean build artifacts
cargo clean
# Update dependencies
cargo update
# Show outdated deps
cargo outdated # Requires cargo-outdated
# Audit for security issues
cargo audit # Requires cargo-auditPro Tips
Use cargo check during development: It's way faster than cargo build and catches most errors.
Install cargo-edit and cargo-watch: They're essential. cargo install cargo-edit cargo-watch
Clippy is your friend: It catches bugs and suggests idioms. Run cc before every commit.
Format automatically: Configure your editor to run rustfmt on save. Consistency matters.
Watch mode always: Keep cwatch running in a tmux pane. Instant feedback beats manual rebuilds.
Use release builds for testing performance: Debug builds are slow. Always benchmark with --release.
Learn the Cargo.toml: Understanding features, workspaces, and profiles will level up your Rust.
Audit dependencies: Run cargo install cargo-audit then cargo audit regularly. Security matters.
Workspace for multi-crate projects: Large projects benefit from Cargo workspaces. One target/ dir, shared dependencies.