|
| 1 | +--- |
| 2 | +status: planned |
| 3 | +created: 2025-12-19 |
| 4 | +priority: medium |
| 5 | +tags: |
| 6 | +- learning |
| 7 | +- rust |
| 8 | +- documentation |
| 9 | +- education |
| 10 | +created_at: 2025-12-19T01:45:10.878889Z |
| 11 | +updated_at: 2025-12-19T01:45:10.878889Z |
| 12 | +--- |
| 13 | + |
| 14 | +# Learning Rust Through LeanSpec Codebase |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +A structured learning path for understanding Rust by exploring the LeanSpec Rust implementation, which was built by AI agents. This spec serves as a guided tour through real-world Rust code, explaining concepts in order of complexity. |
| 19 | + |
| 20 | +## Context |
| 21 | + |
| 22 | +The LeanSpec Rust codebase (`rust/`) contains: |
| 23 | +- **leanspec-core**: Core library with types, parsers, validators, and utilities |
| 24 | +- **leanspec-cli**: Command-line interface |
| 25 | +- **leanspec-mcp**: MCP (Model Context Protocol) server |
| 26 | +- **leanspec-http**: HTTP server |
| 27 | + |
| 28 | +This code was AI-generated and represents practical, production-quality Rust that you can learn from. |
| 29 | + |
| 30 | +## Learning Path |
| 31 | + |
| 32 | +### Phase 1: Rust Fundamentals (Week 1-2) |
| 33 | + |
| 34 | +#### 1.1 Basic Types & Ownership |
| 35 | +**Start here:** [rust/leanspec-core/src/types/spec_info.rs](rust/leanspec-core/src/types/spec_info.rs) |
| 36 | + |
| 37 | +**Concepts to learn:** |
| 38 | +- Structs and enums (`SpecInfo`, `SpecStatus`, `Priority`) |
| 39 | +- Ownership and borrowing (why `&str` vs `String`) |
| 40 | +- Derive macros (`#[derive(Debug, Clone, Serialize)]`) |
| 41 | +- Option types (`Option<String>`, `Option<DateTime<Utc>>`) |
| 42 | + |
| 43 | +**Questions to explore:** |
| 44 | +- Why use `Option<T>` instead of null? |
| 45 | +- When to use `&str` vs `String`? |
| 46 | +- What does `#[serde(rename = "...")]` do? |
| 47 | + |
| 48 | +#### 1.2 Error Handling |
| 49 | +**Next:** [rust/leanspec-core/src/types/mod.rs](rust/leanspec-core/src/types/mod.rs) |
| 50 | + |
| 51 | +**Concepts to learn:** |
| 52 | +- Result type (`Result<T, E>`) |
| 53 | +- Error types with `thiserror` crate |
| 54 | +- The `?` operator for error propagation |
| 55 | +- Custom error types |
| 56 | + |
| 57 | +**Questions to explore:** |
| 58 | +- How does `thiserror` simplify error handling? |
| 59 | +- What's the difference between `unwrap()`, `expect()`, and `?`? |
| 60 | +- How do errors propagate up the call stack? |
| 61 | + |
| 62 | +#### 1.3 Module System |
| 63 | +**Study:** [rust/leanspec-core/src/lib.rs](rust/leanspec-core/src/lib.rs) |
| 64 | + |
| 65 | +**Concepts to learn:** |
| 66 | +- `mod` declarations |
| 67 | +- Public vs private APIs (`pub`, `pub(crate)`) |
| 68 | +- Re-exporting (`pub use`) |
| 69 | +- Module organization |
| 70 | + |
| 71 | +**Questions to explore:** |
| 72 | +- How does Rust's module system differ from JavaScript/TypeScript? |
| 73 | +- What's the purpose of `lib.rs` vs `main.rs`? |
| 74 | +- How do you make internal types public? |
| 75 | + |
| 76 | +### Phase 2: Working with Data (Week 3-4) |
| 77 | + |
| 78 | +#### 2.1 Serialization/Deserialization |
| 79 | +**Study:** [rust/leanspec-core/src/parsers/frontmatter.rs](rust/leanspec-core/src/parsers/frontmatter.rs) |
| 80 | + |
| 81 | +**Concepts to learn:** |
| 82 | +- Serde framework for JSON/YAML |
| 83 | +- Parsing strategies (manual vs derive) |
| 84 | +- Working with unstructured data |
| 85 | +- Validation during deserialization |
| 86 | + |
| 87 | +**Questions to explore:** |
| 88 | +- How does serde work its "magic"? |
| 89 | +- What's the difference between `Serialize` and `Deserialize`? |
| 90 | +- How do you handle optional/missing fields? |
| 91 | + |
| 92 | +#### 2.2 File System Operations |
| 93 | +**Study:** [rust/leanspec-core/src/utils/file_ops.rs](rust/leanspec-core/src/utils/file_ops.rs) (if exists) or search for file operations |
| 94 | + |
| 95 | +**Concepts to learn:** |
| 96 | +- `std::fs` and `std::path` |
| 97 | +- `walkdir` for recursive traversal |
| 98 | +- Path handling (`Path`, `PathBuf`) |
| 99 | +- Error handling for I/O |
| 100 | + |
| 101 | +**Questions to explore:** |
| 102 | +- Why use `PathBuf` vs `Path`? |
| 103 | +- How does Rust handle cross-platform paths? |
| 104 | +- What's the best practice for reading/writing files? |
| 105 | + |
| 106 | +#### 2.3 Collections & Iterators |
| 107 | +**Study:** Search for `Vec`, `HashMap`, `.iter()`, `.map()` usage |
| 108 | + |
| 109 | +**Concepts to learn:** |
| 110 | +- Common collections (`Vec`, `HashMap`, `HashSet`) |
| 111 | +- Iterator patterns (`.iter()`, `.into_iter()`, `.iter_mut()`) |
| 112 | +- Functional programming (`.map()`, `.filter()`, `.collect()`) |
| 113 | +- Iterator chaining |
| 114 | + |
| 115 | +**Questions to explore:** |
| 116 | +- When to use `iter()` vs `into_iter()`? |
| 117 | +- How do iterators achieve zero-cost abstraction? |
| 118 | +- What's the difference between `for x in vec` vs `for x in &vec`? |
| 119 | + |
| 120 | +### Phase 3: Advanced Patterns (Week 5-6) |
| 121 | + |
| 122 | +#### 3.1 CLI Development |
| 123 | +**Study:** [rust/leanspec-cli/src/main.rs](rust/leanspec-cli/src/main.rs) |
| 124 | + |
| 125 | +**Concepts to learn:** |
| 126 | +- Command-line parsing with `clap` |
| 127 | +- Derive macros for CLI (`#[derive(Parser)]`) |
| 128 | +- Subcommands and arguments |
| 129 | +- Interactive prompts with `dialoguer` |
| 130 | + |
| 131 | +**Questions to explore:** |
| 132 | +- How does clap generate help text automatically? |
| 133 | +- What's the derive macro approach vs builder pattern? |
| 134 | +- How do you handle user input safely? |
| 135 | + |
| 136 | +#### 3.2 Async Programming |
| 137 | +**Study:** [rust/leanspec-mcp/src/main.rs](rust/leanspec-mcp/src/main.rs) |
| 138 | + |
| 139 | +**Concepts to learn:** |
| 140 | +- `async`/`await` syntax |
| 141 | +- Tokio runtime |
| 142 | +- Async I/O (stdin/stdout) |
| 143 | +- Concurrent operations |
| 144 | + |
| 145 | +**Questions to explore:** |
| 146 | +- How does async Rust differ from JavaScript Promises? |
| 147 | +- What's the role of the runtime (Tokio)? |
| 148 | +- When to use async vs sync code? |
| 149 | + |
| 150 | +#### 3.3 Graph Algorithms |
| 151 | +**Study:** [rust/leanspec-core/src/utils/dependency_graph.rs](rust/leanspec-core/src/utils/dependency_graph.rs) or search for `petgraph` |
| 152 | + |
| 153 | +**Concepts to learn:** |
| 154 | +- Graph data structures |
| 155 | +- Using `petgraph` library |
| 156 | +- Topological sorting |
| 157 | +- Cycle detection |
| 158 | + |
| 159 | +**Questions to explore:** |
| 160 | +- How do you represent directed graphs in Rust? |
| 161 | +- What algorithms does petgraph provide? |
| 162 | +- How do you detect circular dependencies? |
| 163 | + |
| 164 | +### Phase 4: Production Quality (Week 7-8) |
| 165 | + |
| 166 | +#### 4.1 Testing |
| 167 | +**Study:** Files in `tests/` directories |
| 168 | + |
| 169 | +**Concepts to learn:** |
| 170 | +- Unit tests (`#[cfg(test)]`, `#[test]`) |
| 171 | +- Integration tests (in `tests/` folder) |
| 172 | +- Assertion macros (`assert!`, `assert_eq!`) |
| 173 | +- Test fixtures with `tempfile` |
| 174 | + |
| 175 | +**Questions to explore:** |
| 176 | +- Where do unit vs integration tests go? |
| 177 | +- How do you test file operations? |
| 178 | +- What's `pretty_assertions` for? |
| 179 | + |
| 180 | +#### 4.2 Performance & Optimization |
| 181 | +**Study:** [rust/Cargo.toml](rust/Cargo.toml) `[profile.release]` section |
| 182 | + |
| 183 | +**Concepts to learn:** |
| 184 | +- Release profiles |
| 185 | +- LTO (Link-Time Optimization) |
| 186 | +- Binary size optimization |
| 187 | +- Performance profiling |
| 188 | + |
| 189 | +**Questions to explore:** |
| 190 | +- What do `lto = true` and `opt-level = "z"` do? |
| 191 | +- Why strip symbols in release builds? |
| 192 | +- How to measure performance? |
| 193 | + |
| 194 | +#### 4.3 Cross-Platform Distribution |
| 195 | +**Study:** [rust/npm-dist/](rust/npm-dist/) wrapper scripts |
| 196 | + |
| 197 | +**Concepts to learn:** |
| 198 | +- Cross-compilation |
| 199 | +- npm integration for Rust binaries |
| 200 | +- Platform-specific binary selection |
| 201 | +- Distribution strategies |
| 202 | + |
| 203 | +**Questions to explore:** |
| 204 | +- How do you distribute Rust binaries via npm? |
| 205 | +- What's the wrapper script pattern? |
| 206 | +- How to handle different architectures? |
| 207 | + |
| 208 | +## Learning Resources by Concept |
| 209 | + |
| 210 | +### For Each Code File You Study |
| 211 | + |
| 212 | +1. **Read First**: Understand the overall purpose |
| 213 | +2. **Identify Patterns**: Look for Rust idioms |
| 214 | +3. **Compare**: How would this be done in JavaScript/TypeScript? |
| 215 | +4. **Experiment**: Clone and modify the code locally |
| 216 | +5. **Document**: Write notes on confusing parts |
| 217 | + |
| 218 | +### External Resources |
| 219 | + |
| 220 | +- **The Rust Book**: For fundamentals (https://doc.rust-lang.org/book/) |
| 221 | +- **Rust by Example**: For practical patterns (https://doc.rust-lang.org/rust-by-example/) |
| 222 | +- **API Docs**: Use `cargo doc --open` to generate docs for this project |
| 223 | +- **Crate Docs**: Check docs.rs for dependencies (serde, clap, tokio, etc.) |
| 224 | + |
| 225 | +### Suggested Workflow |
| 226 | + |
| 227 | +```bash |
| 228 | +# Setup your learning environment |
| 229 | +cd rust/ |
| 230 | + |
| 231 | +# Build and explore |
| 232 | +cargo build # Compile everything |
| 233 | +cargo doc --open # Generate and view docs |
| 234 | +cargo clippy # Check for common mistakes |
| 235 | +cargo test # Run tests |
| 236 | + |
| 237 | +# Try modifying code |
| 238 | +# 1. Make a small change to leanspec-core |
| 239 | +# 2. Run tests to see if it breaks |
| 240 | +# 3. Fix any errors |
| 241 | +# 4. Learn from compiler messages! |
| 242 | + |
| 243 | +# Use the CLI to understand behavior |
| 244 | +cargo run --bin leanspec-cli -- --help |
| 245 | +cargo run --bin leanspec-cli -- list |
| 246 | +``` |
| 247 | + |
| 248 | +## Key Rust Concepts Used in LeanSpec |
| 249 | + |
| 250 | +### Ownership & Borrowing |
| 251 | +- **Where**: Everywhere! Functions take `&SpecInfo` vs `SpecInfo` |
| 252 | +- **Why**: Memory safety without garbage collection |
| 253 | +- **Learn**: Pay attention to function signatures |
| 254 | + |
| 255 | +### Pattern Matching |
| 256 | +- **Where**: Handling `Result`, `Option`, enums |
| 257 | +- **Why**: Exhaustive, safe control flow |
| 258 | +- **Learn**: Look for `match` expressions |
| 259 | + |
| 260 | +### Traits |
| 261 | +- **Where**: `impl` blocks, trait bounds |
| 262 | +- **Why**: Polymorphism and code reuse |
| 263 | +- **Learn**: Common traits (Debug, Clone, Serialize) |
| 264 | + |
| 265 | +### Lifetimes |
| 266 | +- **Where**: Functions that return references |
| 267 | +- **Why**: Ensuring references are valid |
| 268 | +- **Learn**: Usually inferred, explicit when needed |
| 269 | + |
| 270 | +### Zero-Cost Abstractions |
| 271 | +- **Where**: Iterators, generics |
| 272 | +- **Why**: High-level code, low-level performance |
| 273 | +- **Learn**: Iterator chains compile to efficient loops |
| 274 | + |
| 275 | +## Exercises |
| 276 | + |
| 277 | +### Beginner |
| 278 | +- [ ] Add a new field to `SpecInfo` struct |
| 279 | +- [ ] Create a new `Priority` variant |
| 280 | +- [ ] Write a function to filter specs by tag |
| 281 | +- [ ] Add a new CLI command (like `lean-spec hello`) |
| 282 | + |
| 283 | +### Intermediate |
| 284 | +- [ ] Implement a new validator (e.g., check spec title length) |
| 285 | +- [ ] Add a new search filter option |
| 286 | +- [ ] Create a utility function for spec manipulation |
| 287 | +- [ ] Parse a new frontmatter field format |
| 288 | + |
| 289 | +### Advanced |
| 290 | +- [ ] Add a new MCP tool |
| 291 | +- [ ] Implement spec dependency cycle detection |
| 292 | +- [ ] Optimize search performance with indexing |
| 293 | +- [ ] Add parallel processing for large spec sets |
| 294 | + |
| 295 | +## Rust vs TypeScript: Key Differences |
| 296 | + |
| 297 | +| Concept | TypeScript | Rust | |
| 298 | +|---------|-----------|------| |
| 299 | +| **Memory** | Garbage collected | Ownership system | |
| 300 | +| **Null** | `null`/`undefined` | `Option<T>` | |
| 301 | +| **Errors** | Exceptions | `Result<T, E>` | |
| 302 | +| **Types** | Gradual typing | Strong static typing | |
| 303 | +| **Concurrency** | Event loop | Threads + async | |
| 304 | +| **Compile time** | Type checking | Type + borrow checking | |
| 305 | +| **Performance** | Runtime overhead | Zero-cost abstractions | |
| 306 | + |
| 307 | +## Compiler as Teacher |
| 308 | + |
| 309 | +The Rust compiler is famously helpful! When you get an error: |
| 310 | +1. **Read the full message** - It explains what's wrong |
| 311 | +2. **Check suggestions** - Often includes fixes |
| 312 | +3. **Look up error code** - `rustc --explain E0308` |
| 313 | +4. **Iterate** - Fix one error at a time |
| 314 | + |
| 315 | +## Expected Outcomes |
| 316 | + |
| 317 | +After completing this learning path, you should be able to: |
| 318 | + |
| 319 | +- ✅ Read and understand the LeanSpec Rust codebase |
| 320 | +- ✅ Modify existing functionality |
| 321 | +- ✅ Add new features to CLI, MCP, or core |
| 322 | +- ✅ Write tests for Rust code |
| 323 | +- ✅ Understand Rust's ownership and borrowing |
| 324 | +- ✅ Use common Rust patterns and idioms |
| 325 | +- ✅ Debug Rust programs effectively |
| 326 | +- ✅ Compare Rust vs TypeScript trade-offs |
| 327 | + |
| 328 | +## Success Metrics |
| 329 | + |
| 330 | +- Can explain ownership in 3 sentences or less |
| 331 | +- Can add a new CLI command without copying code blindly |
| 332 | +- Can fix a bug in the core library |
| 333 | +- Can explain why Rust is faster than Node.js |
| 334 | +- Feel comfortable reading Rust documentation |
| 335 | + |
| 336 | +## Next Steps |
| 337 | + |
| 338 | +After mastering this codebase: |
| 339 | +1. Contribute improvements to LeanSpec |
| 340 | +2. Build your own Rust CLI tool |
| 341 | +3. Explore async Rust more deeply |
| 342 | +4. Study systems programming concepts |
| 343 | +5. Join the Rust community |
| 344 | + |
| 345 | +## References |
| 346 | + |
| 347 | +- LeanSpec Rust README: [rust/README.md](rust/README.md) |
| 348 | +- Spec #170: [170-cli-mcp-core-rust-migration-evaluation](../170-cli-mcp-core-rust-migration-evaluation/README.md) |
| 349 | +- Spec #172: [172-rust-cli-mcp-npm-distribution](../172-rust-cli-mcp-npm-distribution/README.md) |
| 350 | +- Spec #181: [181-typescript-deprecation-rust-migration](../181-typescript-deprecation-rust-migration/README.md) |
| 351 | + |
| 352 | +--- |
| 353 | + |
| 354 | +**Remember**: The best way to learn Rust is to write Rust. Start small, experiment often, and trust the compiler to guide you! |
0 commit comments