Files
marshal-rust/01-introduction-to-rust/README.md
T
kiskaadee b768383767 docs(study): add active-learning template, Ch 1 example, and Ch 2 NixOS setup
- 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
2026-11-27 23:58:09 -05:00

39 lines
3.0 KiB
Markdown

# 1. Introduction to Rust
> **Reference:** Donis Marshall, *Programming with Rust* (2024) — Chapter 1
> **Phase:** 1 (Basics & Tooling) | **Code:** [`mylib/src/listings.rs`](./mylib/src/listings.rs), [`mylib/src/lib.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`, and `match` return values rather than just mutating state (omit the trailing `;` to return).
- *Pattern-oriented:* `match` destructures 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.toml` manifest, `Cargo.lock` exact 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`](./mylib/src/listings.rs):*
- **Experiment 1 (Trailing semicolon in an expression):** Add `;` after `1` or `n * factorial(n - 1)` inside `factorial`.
- **Result / `rustc` message:** Type mismatch—expected `i64`, found `()` (unit type), because `;` turns an expression into a statement.
- **Takeaway:** In Rust, the final expression without `;` *is* the return value.
- **Experiment 2 (Non-exhaustive `match`):** Remove the `_ => ...` arm in `factorial`.
- **Result / `rustc` message:** Compile error: `non-exhaustive patterns: i64::MIN..=-1_i64 and 2_i64..=i64::MAX not covered`.
- **Takeaway:** `match` won't compile if any possible input is unhandled.
## 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.toml` project that can contain multiple binary crates and at most one library crate.
- **File layout note:** `listings.rs` inside `mylib/src/` isn't compiled automatically unless declared as a module (`mod listings;`) in `lib.rs` or placed under `src/bin/`.
## 4. Recall Prompts (Answer Without Looking)
1. What is the difference between a **statement** and an **expression** in Rust, and how does `;` change it?
2. How do **crate**, **package**, and **module** relate to each other?
3. What does the **borrow checker** enforce at compile time that garbage-collected languages handle at runtime (or not at all)?