Rust starter example project.
# Install rustup via Homebrew
brew install rustup
# Install the Rust toolchain and setup shell profile
rustup-init
# Update to latest stable Rust compiler and tools
rustup update stableuse rust_starter::{is_leap_year, days_in_month};
// Check leap years
assert!(is_leap_year(2024));
assert!(!is_leap_year(2025));
// Get the number of days in a month
assert_eq!(days_in_month(2024, 2), Some(29));
assert_eq!(days_in_month(2025, 2), Some(28));
assert_eq!(days_in_month(2025, 4), Some(30));# Compile the project (debug mode)
cargo build
# Compile with optimizations (release mode)
cargo build --release
# Compile and run
cargo run# Format code
cargo fmtClippy is the official Rust linter.
# Run clippy
cargo clippy
# Auto-fix
cargo clippy --no-deps --fix --allow-dirtyCargo comes with a comprehensive test runner
# Run all tests with standard cargo test
cargo testcargo-nextest is an alternative with prettier output and some extra features like code coverage.
cargo install --locked cargo-nextest
# Run tests with nextest
cargo nextest runCode coverage is generated using cargo-llvm-cov.
cargo install --locked cargo-llvm-cov# Run tests with coverage (text output)
cargo llvm-cov nextest
# Generate HTML coverage report
cargo llvm-cov nextest --html
# Generate and open HTML report in browser
cargo llvm-cov nextest --html --openThe HTML report is generated in target/llvm-cov/html/.
# Add a dependency
cargo add serde
# Add a dependency with features
cargo add serde --features derive
# Add a dev-only dependency
cargo add --dev assert_cmd
# Remove a dependency
cargo remove serde
# Update dependencies to latest compatible versions (only updates Cargo.lock)
cargo update
# Upgrade dependency version requirements in Cargo.toml
# Requires cargo-edit: cargo install cargo-edit
cargo upgrade
# Update major versions / breaking changes
cargo upgrade --incompatible
# View dependency tree
cargo tree
# Generate and open documentation for your project and dependencies
cargo doc --open
# Clean build artifacts
cargo cleanTip
Pro-tip: add this shell alias for running everything with one command:
cargocheck='cargo fmt && cargo clippy --no-deps --fix --allow-dirty && cargo fmt && cargo build && cargo test'
This project uses prek to run formatting and linting checks before each commit, replacing pre-commit.
# Install prek
brew install prek
# Set up the hooks to run on git commit.
# Use -f once if this repo already has an older Git hook shim installed.
prek install -f
# Run all hooks manually against all files
prek run --all-files