Files
rustlings/README.md

352 lines
9.7 KiB
Markdown

# Rustlings - Learn Rust the Fun Way! 🦀
A collection of small exercises to get you familiar with reading and writing Rust code. This is the completed solutions version where all 94 exercises have been solved.
## Table of Contents
- [Overview](#overview)
- [Exercise Categories](#exercise-categories)
- [Installation & Setup](#installation--setup)
- [Running Exercises](#running-exercises)
- [Project Structure](#project-structure)
- [Exercise Completion Status](#exercise-completion-status)
- [Learning Path](#learning-path)
- [Resources](#resources)
## Overview
Rustlings is a collection of small, bite-sized exercises designed to teach you the fundamentals of Rust. Each exercise focuses on a specific concept and provides a hands-on learning experience. This repository contains all 94 exercises fully solved, organized into 24 different topics.
The course progresses from the absolute basics (variables, functions) through intermediate concepts (ownership, traits, lifetimes) to advanced topics (smart pointers, macros, error handling).
## Exercise Categories
### Core Fundamentals (27 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **00_intro** | 2 | Basic syntax, println! macro |
| **01_variables** | 6 | Variable declaration, mutability, shadowing, const |
| **02_functions** | 5 | Function definitions, parameters, return types |
| **03_if** | 3 | Conditional logic, if-else expressions |
| **04_primitive_types** | 6 | Booleans, characters, arrays, slices, tuples |
### Collections & Ownership (12 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **05_vecs** | 2 | Vector creation, iteration, modification |
| **06_move_semantics** | 5 | Ownership, borrowing, references |
| **07_structs** | 3 | Struct definition, initialization, methods |
| **08_enums** | 3 | Enum variants, pattern matching |
### Strings & Collections (10 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **09_strings** | 4 | String types, conversion, manipulation |
| **10_modules** | 3 | Module system, visibility, imports |
| **11_hashmaps** | 3 | HashMap operations, entry API |
### Type System & Traits (16 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **12_options** | 3 | Option type, pattern matching |
| **13_error_handling** | 6 | Result type, error propagation, ? operator |
| **14_generics** | 2 | Generic type parameters |
| **15_traits** | 5 | Trait definition, implementation, objects |
### Advanced Concepts (22 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **16_lifetimes** | 3 | Lifetime annotations, scope |
| **17_tests** | 3 | Writing and running tests |
| **18_iterators** | 5 | Iterator trait, adapters, consumers |
| **19_smart_pointers** | 4 | Box, Rc, Arc, Cow smart pointers |
| **20_threads** | 3 | Thread spawning, synchronization, channels |
| **21_macros** | 4 | Macro definition, visibility |
| **22_clippy** | 3 | Code linting, clippy recommendations |
### Conversions & Quizzes (8 exercises)
| Topic | Exercises | Concepts |
|-------|-----------|----------|
| **23_conversions** | 5 | From/Into, TryFrom, FromStr, AsRef/AsMut |
| **quizzes** | 3 | Comprehensive knowledge checks |
## Installation & Setup
### Requirements
- **Rust 1.56+** (Latest stable recommended)
- **Cargo** (comes with Rust)
### Getting Started
1. **Clone or extract the repository**
```bash
cd rustlings
```
2. **Verify Rust installation**
```bash
rustc --version
cargo --version
```
3. **Check the Cargo configuration**
```bash
cat Cargo.toml
```
## Running Exercises
### Running All Tests
To verify all solutions are correct:
```bash
cargo test
```
### Running Specific Exercise Tests
To test a specific topic:
```bash
# Test a specific category
cargo test --test 01_variables
# Test a specific exercise
cargo test variables1
```
### Viewing Exercise Details
Each exercise directory contains:
- Individual `.rs` files with code
- `README.md` with instructions and hints
- Tests in the code to verify correctness
```bash
# View exercise instructions
cat exercises/01_variables/README.md
# Check a specific exercise file
cat exercises/01_variables/variables1.rs
```
## Project Structure
```
rustlings/
├── Cargo.toml # Project manifest
├── rust-analyzer.toml # Rust analyzer configuration
├── README.md # This file
├── exercises/ # All exercise files
│ ├── 00_intro/ # Introduction exercises
│ ├── 01_variables/ # Variable exercises
│ ├── 02_functions/ # Function exercises
│ ├── ...
│ ├── 22_clippy/ # Code linting exercises
│ ├── 23_conversions/ # Type conversion exercises
│ └── quizzes/ # Comprehensive quizzes
├── solutions/ # Reference solutions
│ ├── 00_intro/
│ ├── 01_variables/
│ └── ...
└── target/ # Compiled artifacts (auto-generated)
```
### Exercise File Format
Each exercise file typically contains:
```rust
// Description of what to learn
// TODO: instructions for what to implement
fn main() {
// Your code here
}
#[cfg(test)]
mod tests {
// Tests that verify your solution
}
```
## Exercise Completion Status
### Summary
- **Total Exercises:** 94
- **Status:** ✅ All 100% Complete
### By Category
✅ **Intro** - 2/2
✅ **Variables** - 6/6
✅ **Functions** - 5/5
✅ **If Statements** - 3/3
✅ **Primitive Types** - 6/6
✅ **Vectors** - 2/2
✅ **Move Semantics** - 5/5
✅ **Structs** - 3/3
✅ **Enums** - 3/3
✅ **Strings** - 4/4
✅ **Modules** - 3/3
✅ **Hashmaps** - 3/3
✅ **Options** - 3/3
✅ **Error Handling** - 6/6
✅ **Generics** - 2/2
✅ **Traits** - 5/5
✅ **Lifetimes** - 3/3
✅ **Tests** - 3/3
✅ **Iterators** - 5/5
✅ **Smart Pointers** - 4/4
✅ **Threads** - 3/3
✅ **Macros** - 4/4
✅ **Clippy** - 3/3
✅ **Conversions** - 5/5
✅ **Quizzes** - 3/3
## Learning Path
### Recommended Order
**Phase 1: Foundations (Days 1-2)**
1. 00_intro - Learn basic syntax
2. 01_variables - Understand variable binding
3. 02_functions - Write functions
4. 03_if - Control flow
5. 04_primitive_types - Data types
**Phase 2: Collections & Ownership (Days 3-4)**
6. 05_vecs - Collections
7. 06_move_semantics - Rust's unique ownership system
8. 07_structs - Custom types
9. 08_enums - Sum types
**Phase 3: Strings & Modules (Days 5)**
10. 09_strings - String handling
11. 10_modules - Code organization
12. 11_hashmaps - More collections
**Phase 4: Type System (Days 6-7)**
13. 12_options - Null safety
14. 13_error_handling - Error management
15. 14_generics - Generic programming
16. 15_traits - Interface abstraction
**Phase 5: Advanced (Days 8-10)**
17. 16_lifetimes - Reference lifetimes
18. 17_tests - Testing
19. 18_iterators - Functional programming
20. 19_smart_pointers - Memory management
21. 20_threads - Concurrency
22. 21_macros - Metaprogramming
23. 22_clippy - Code quality
24. 23_conversions - Type conversions
25. quizzes - Assessment
## Key Concepts Covered
### Ownership & Borrowing
- Move semantics
- References and borrowing
- Lifetimes
- Smart pointers (Box, Rc, Arc, Cow)
### Type System
- Pattern matching
- Enums and Options
- Result for error handling
- Generics and traits
- Type conversions
### Collections
- Vectors
- Strings
- Hashmaps
### Advanced Topics
- Iterators and functional programming
- Testing
- Macros
- Threading and concurrency
- Error handling patterns
## Resources
### Official Documentation
- [The Rust Book](https://doc.rust-lang.org/book/) - Comprehensive guide
- [Rust API Documentation](https://docs.rs/) - Standard library docs
- [Rust by Example](https://doc.rust-lang.org/rust-by-example/) - Practical examples
### Learning
- [Rust Playground](https://play.rust-lang.org/) - Try Rust online
- [Clippy Lint Documentation](https://doc.rust-lang.org/clippy/) - Code quality
- [Rustlings Official](https://github.com/rust-lang/rustlings) - Original project
### Tools
- [rust-analyzer](https://rust-analyzer.github.io/) - IDE support
- [rustfmt](https://github.com/rust-lang/rustfmt) - Code formatter
- [clippy](https://github.com/rust-lang/rust-clippy) - Linter
## Tips for Learning
1. **Read the README in each exercise directory** - They provide valuable context
2. **Understand the error messages** - Rust's compiler is very helpful
3. **Don't just copy solutions** - Type them out to build muscle memory
4. **Run tests frequently** - `cargo test` is your friend
5. **Experiment with variations** - Try modifying working solutions
6. **Use rust-analyzer** - Get IDE support for better learning
## Common Commands
```bash
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Check code without running
cargo check
# Format code
cargo fmt
# Run clippy linter
cargo clippy
# Build release version
cargo build --release
# View test file details
cargo test --doc
```
## Contributing
This is a completed version of Rustlings. To:
- **Learn from solutions**: Examine the exercise files in `exercises/`
- **Review solutions**: Check the `solutions/` directory
- **Understand concepts**: Read the README files in each exercise category
## License
Rustlings is licensed under the MIT license.
## Acknowledgments
Rustlings was created by the Rust community to help new learners understand Rust concepts through practical exercises. This completed version includes all solutions for reference and learning purposes.
---
**Happy Learning!** 🎓🦀
If you have questions about specific concepts, refer to the official Rust documentation or the README files in each exercise directory.