A collection of Rust learning projects demonstrating different concepts and best practices.
- Purpose: First Rust project with basic "Hello, world!" program
- Concepts: Basic Rust syntax,
cargo new,cargo run - Learning: Getting started with Rust development
- Purpose: Understanding Cargo project structure and build system
- Concepts:
Cargo.toml,Cargo.lock, project organization - Learning: How Cargo manages dependencies and builds
- Purpose: Working with external crates and advanced Rust features
- Concepts:
- Adding dependencies (
randcrate) - Constants for magic numbers
- Documentation comments (
///) - Error handling with
Result<T, E> - Unit tests with
#[test] - Development tools (CodeLLDB, Even Better TOML)
- Adding dependencies (
- Learning: Production-ready Rust development practices
- Start with
hello_rust- Basic Rust syntax and Cargo - Move to
hello_cargo- Understanding project structure - Complete with
hello_dep_ran- Advanced concepts and best practices - Explore
hello_sum_calc- Clippy linting and rust-analyzer configuration
All projects are configured with:
- Cursor/VS Code with Rust extensions
- rust-analyzer for language support
- CodeLLDB for debugging
- Even Better TOML for configuration files
To enable a faster development cycle with automatic recompilation and re-running of your Rust project upon file changes, you can use cargo-watch. This tool provides "hot reload" functionality, giving you immediate feedback on your code changes.
Install cargo-watch globally so you can use the cargo watch command:
cargo install cargo-watchNote: This installs the tool globally, making cargo watch available as a command. Adding it as a dependency with cargo add cargo-watch only makes it available to your project, but doesn't install the binary tool.
Once installed, you can use cargo-watch to monitor your source files and automatically re-run your application. Here's a common command:
cargo watch -q -c -w src/ -x 'run -q'Explanation of the flags:
-q(quiet): Reduces the output fromcargo-watchitself, keeping your terminal cleaner-c(clear): Clears the terminal screen before each new run of your application, providing a fresh output-w src/(watch directory): Specifies thatcargo-watchshould monitor thesrc/directory for any file changes. This is where your Rust source code resides-x 'run -q'(execute command): This is the command thatcargo-watchwill execute whenever it detects changesrun: Tells Cargo to compile and run your project-q: Makes thecargo runcommand also run in quiet mode, further reducing output
This setup allows you to save your Rust files, and cargo-watch will automatically recompile and re-run your application, providing a seamless hot-reloading experience.
# Navigate to any project
cd hello_rust
cd hello_cargo
cd hello_dep_ran
# Run the project
cargo run
# Run tests (where applicable)
cargo test
# Check for errors
cargo check