Skip to content

NitorCreations/rust-starter

Repository files navigation

Rust project starter

Rust starter example project.

Install Rust

# 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 stable

Usage Example

use 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));

Build

# Compile the project (debug mode)
cargo build

# Compile with optimizations (release mode)
cargo build --release

# Compile and run
cargo run

Formatting

# Format code
cargo fmt

Linting

Clippy is the official Rust linter.

# Run clippy
cargo clippy

# Auto-fix
cargo clippy --no-deps --fix --allow-dirty

Tests

Cargo comes with a comprehensive test runner

# Run all tests with standard cargo test
cargo test

cargo-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 run

Code Coverage

Code 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 --open

The HTML report is generated in target/llvm-cov/html/.

Useful Cargo Commands

# 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 clean

Tip

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'

Commit Hooks

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

About

Example Rust project with tooling configs and usage instructions

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages