mirror of
https://github.com/learning-forge/marshal-rust.git
synced 2026-11-30 21:30:31 +00:00
- Add .gitignore to exclude local book PDFs and compiled Rust artifacts - Document reference book metadata and 3-part study loop in README.md and NOTES_TEMPLATE.md - Rewrite 01-introduction-to-rust/README.md as a concise active-learning example with mylib/ listings - Adapt 02-getting-started/README.md to template with NixOS Home Manager toolchain setup
1. Introduction to Rust
Reference: Donis Marshall, Programming with Rust (2024) — Chapter 1 Phase: 1 (Basics & Tooling) | Code:
mylib/src/listings.rs,mylib/src/lib.rs
1. Mental Model (My Own Words)
- Why Rust exists: Memory and concurrency safety without a garbage collector. Instead of catching bugs at runtime (or via manual C/C++ discipline), Rust shifts those checks to compile time via the borrow checker.
- Three paradigms combined:
- Functional-light: Functions are first-class values; immutability is the default to limit side effects.
- Expression-oriented: Blocks,
if, andmatchreturn values rather than just mutating state (omit the trailing;to return). - Pattern-oriented:
matchdestructures tuples, enums, and structs—and the compiler enforces exhaustiveness (you must handle every case).
- Safety pillars: Single-owner principle (Ownership), temporary access (Borrowing), compile-time validity scopes (Lifetimes), RAII cleanup when values leave scope, and zero-cost abstractions (safety checks compile away, no runtime tax).
- Toolchain & unit map:
rustup(toolchain manager) →cargo(package/build manager,Cargo.tomlmanifest,Cargo.lockexact versions) →rustc(compiler) +clippy(linter) +rustfmt(formatter).- Module (namespace) ⊂ Crate (compilation unit: binary or library) ⊂ Package (1+ crates sharing a
Cargo.toml).
2. Break-It Experiments
Tested in mylib/src/listings.rs:
- Experiment 1 (Trailing semicolon in an expression): Add
;after1orn * factorial(n - 1)insidefactorial.- Result /
rustcmessage: Type mismatch—expectedi64, found()(unit type), because;turns an expression into a statement. - Takeaway: In Rust, the final expression without
;is the return value.
- Result /
- Experiment 2 (Non-exhaustive
match): Remove the_ => ...arm infactorial.- Result /
rustcmessage: Compile error:non-exhaustive patterns: i64::MIN..=-1_i64 and 2_i64..=i64::MAX not covered. - Takeaway:
matchwon't compile if any possible input is unhandled.
- Result /
3. Friction Log ("Gotchas")
- Terminology trap — Crate vs. Package: A "crate" is not a whole project; it's a single compilation unit (one binary or one library). A package is the
Cargo.tomlproject that can contain multiple binary crates and at most one library crate. - File layout note:
listings.rsinsidemylib/src/isn't compiled automatically unless declared as a module (mod listings;) inlib.rsor placed undersrc/bin/.
4. Recall Prompts (Answer Without Looking)
- What is the difference between a statement and an expression in Rust, and how does
;change it? - How do crate, package, and module relate to each other?
- What does the borrow checker enforce at compile time that garbage-collected languages handle at runtime (or not at all)?