A tutorial repository with hands-on exercises to learn Rust programming.
-
Clone the repository:
git clone https://github.com/achuvyas-kv/rust-learning-sessions.git cd rust-learning-sessions -
Run the project:
cargo run
File: src/exercise_01.rs
Task: Implement the add_numbers function that takes two integers and returns their sum.
Solution: SOLUTION_exercise_01.rs (for reference after attempting)
To solve:
- Open
src/exercise_01.rs - Find the
add_numbersfunction - Replace the
todo!()macro with your implementation - Run the tests to check your solution
Run tests:
# Run all tests
cargo test
# Run only exercise 01 tests
cargo test exercise_01
# Run tests with output (even for passing tests)
cargo test -- --nocaptureEach exercise follows this pattern:
- File:
src/exercise_XX.rs(where XX is the exercise number) - Function: Contains a
todo!()macro that you need to replace - Tests: Comprehensive test cases to verify your solution
- Documentation: Clear instructions and examples
The repository uses Rust's built-in testing framework. Each exercise includes:
- Multiple test cases covering different scenarios
- Clear error messages when tests fail
- Examples of expected behavior
# Run all tests
cargo test
# Run specific exercise tests (using test name pattern)
cargo test exercise_01 # Exercise 01
cargo test exercise_02 # Exercise 02 (when available)
# Run tests with verbose output
cargo test -- --nocapture
# Run tests and show output even for passing tests
cargo test -- --nocapture --test-threads=1To add a new exercise:
- Create
src/exercise_XX.rswith your exercise - Add the module to
src/main.rs - Update this README with exercise details
- Ensure tests can be run with
cargo test exercise_XX
- Start with Exercise 01 - Basic function implementation
- Read the documentation in each exercise file
- Try to solve without looking at solutions
- Run tests to verify your solution
- Move to the next exercise when all tests pass
- Use
cargo checkto check for compilation errors without running tests - Use
cargo fmtto format your code - Use
cargo clippyfor additional linting suggestions - Don't be afraid to experiment and make mistakes!
Feel free to add more exercises or improve existing ones. Make sure to:
- Include comprehensive tests
- Provide clear documentation
- Follow Rust coding conventions
- Update the README with new exercises
Happy coding! 🦀