diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3cb0f9c --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Reference books / copyrighted course material (kept locally only) +*.pdf + +# Rust build artifacts & ad-hoc rustc binaries +target/ +**/*.rs.bk +01-introduction-to-rust/mylib/src/listings diff --git a/01-introduction-to-rust/README.md b/01-introduction-to-rust/README.md new file mode 100644 index 0000000..c3a4876 --- /dev/null +++ b/01-introduction-to-rust/README.md @@ -0,0 +1,38 @@ +# 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)? diff --git a/01-introduction-to-rust/mylib/Cargo.toml b/01-introduction-to-rust/mylib/Cargo.toml new file mode 100644 index 0000000..bf0d4b1 --- /dev/null +++ b/01-introduction-to-rust/mylib/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "mylib" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/01-introduction-to-rust/mylib/src/lib.rs b/01-introduction-to-rust/mylib/src/lib.rs new file mode 100644 index 0000000..b93cf3f --- /dev/null +++ b/01-introduction-to-rust/mylib/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/01-introduction-to-rust/mylib/src/listings.rs b/01-introduction-to-rust/mylib/src/listings.rs new file mode 100644 index 0000000..92c61b1 --- /dev/null +++ b/01-introduction-to-rust/mylib/src/listings.rs @@ -0,0 +1,23 @@ +fn main(){ + println!("{}",factorial(15)); + display_firstname(("Stark", "Tony")) +} + +fn factorial(n:i64) -> i64 { + // Code Listing 1.1. Factorial is a pure function and expression + match n { + 0..=1=>1, + _=> n * factorial(n - 1) + } +} + + + +fn display_firstname(name: (&str, &str)){ + // Code listing 1.2. The display_firstname function displays a first name + match name{ + (_, first) => println!("{}", first), + + } +} + diff --git a/02-getting-started/README.md b/02-getting-started/README.md new file mode 100644 index 0000000..616486d --- /dev/null +++ b/02-getting-started/README.md @@ -0,0 +1,39 @@ +# 2. Getting Started + +> **Reference:** Donis Marshall, *Programming with Rust* (2024) — Chapter 2 +> **Phase:** 1 (Basics & Tooling) | **Code:** *(add crate/file link as you go)* + +## 1. Mental Model (My Own Words) + +- **Core idea:** Getting a working Rust toolchain, compiling with `rustc` vs. managing projects with `cargo`, and understanding the entry point (`main`), libraries, comments, and CLI arguments. +- **Installing Rust on NixOS (instead of `rustup`):** + - On macOS/Windows/traditional Linux, `rustup` downloads toolchains into `~/.cargo/bin` and switches between `stable`, `beta`, and `nightly`. + - On NixOS, **Home Manager** manages the global toolchain declaratively in `~/Config/home/dev.nix` (`cargo`, `rustc`, `rustfmt`, `clippy`, `rust-analyzer`, and `gcc` as linker, exposed in `/etc/profiles/per-user/$USER/bin/`). Installing `rustup` alongside Nix-managed binaries is redundant and risks `PATH` shadowing. + - **Per-project overrides:** When a project needs a pinned Rust version or cross-compilation targets (what the book calls "Advanced Rustup"), use a per-project `flake.nix` `devShell` instead of `rustup toolchain`. + - **User-installed binaries:** `~/.cargo/bin` stays on `PATH` for tools installed via `cargo install`. +- **Remaining Chapter 2 topics to distill as I read:** + - `rustc` direct compile vs. `cargo new` / `cargo build` / `cargo run` + - Binary (`src/main.rs`) vs. Library (`src/lib.rs`) crates and published crates (`crates.io`) + - `main()` signatures, doc comments (`///`), and `std::env::args()` + +## 2. Break-It Experiments + +*Ideas to test in `cargo` as I work through the chapter:* + +- **Experiment 1 (`rustc` vs `cargo`):** Compile a standalone `hello.rs` with `rustc`, then create a Cargo package and compare what `Cargo.toml` / `target/` generate. + - **Result / `rustc` message:** *(fill in)* + - **Takeaway:** *(fill in)* +- **Experiment 2 (Tooling checks):** Run `rustc --version`, `cargo --version`, `cargo clippy`, and `cargo fmt --check` on a tiny crate. + - **Result:** *(fill in)* + - **Takeaway:** *(fill in)* + +## 3. Friction Log ("Gotchas") + +- **NixOS `clippy` binary name:** `which clippy` fails because the subcommand binary is `cargo-clippy`—always invoke it as `cargo clippy`. +- *(Add compiler/Cargo gotchas here as you hit them.)* + +## 4. Recall Prompts (Answer Without Looking) + +1. Why do we avoid `rustup` on NixOS when Home Manager already provides `rustc` and `cargo`, and what replaces `rustup` for pinned per-project toolchains? +2. What is the difference between running `rustc main.rs` directly vs. `cargo run`? +3. Where do `cargo install` binaries live, and how does `PATH` find them? diff --git a/NOTES_TEMPLATE.md b/NOTES_TEMPLATE.md new file mode 100644 index 0000000..24b6142 --- /dev/null +++ b/NOTES_TEMPLATE.md @@ -0,0 +1,29 @@ +# . + +> **Reference:** Donis Marshall, *Programming with Rust* (2024) — Chapter +> **Phase:** <1–6> | **Code:** [``](./) + +## 1. Mental Model (My Own Words) + +- **Core idea:** +- **Why it matters / problem it solves:** +- **Key distinction:** (e.g., how this differs from Python/JS or earlier mental models) + +## 2. Break-It Experiments + +*What did I try in `cargo`—especially what did I intentionally break to see `rustc` push back?* + +- **Experiment:** + - **Result / `rustc` message:** + - **Takeaway:** + +## 3. Friction Log ("Gotchas") + +- **Misconception / Error:** + - **Fix / Rule:** + +## 4. Recall Prompts (Answer Without Looking) + +1. +2. +3. diff --git a/README.md b/README.md index ef7f3b4..54e4484 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,21 @@ # Programming with Rust -Study notes from the Donis Marshall book [ISBN-13: 978-0-13-7889 65-5] +Personal study notes, compiler experiments, and exercises following **Donis Marshall, *Programming with Rust* (Pearson Education, 2024)**. + +## Reference Material + +- **Book:** *Programming with Rust* by Donis Marshall +- **Publisher / Year:** Pearson Education / Addison-Wesley Professional (2024) +- **ISBN-13:** `978-0-13-788965-5` +- **Note:** The book PDF is kept locally for personal study and is intentionally excluded from git tracking via `.gitignore`. + +## Study Methodology + +Instead of transcribing chapters verbatim, each chapter follows a 3-part active loop (see [`NOTES_TEMPLATE.md`](./NOTES_TEMPLATE.md) and [`01-introduction-to-rust/README.md`](./01-introduction-to-rust/README.md) for an example): + +1. **Mental Model (3–5 bullets in my own words):** What problem does this solve, and how does it differ from dynamic languages? +2. **Break-It Experiments:** Run code in `cargo` and intentionally break rules to read `rustc`'s error messages. +3. **Friction Log & Recall Prompts:** Record actual compiler errors/misconceptions and 2–3 questions to answer from memory. --- @@ -8,7 +23,7 @@ Study notes from the Donis Marshall book [ISBN-13: 978-0-13-7889 65-5] ## Contents: -### 1. Introduction to Rust +### [1. Introduction to Rust](/01-introduction-to-rust/README.md) - Introduction * Functional Programming @@ -25,7 +40,7 @@ Study notes from the Donis Marshall book [ISBN-13: 978-0-13-7889 65-5] * A Note About Securit - Summary -### 2. Getting Started +### [2. Getting Started](/02-getting-started/README.md) - Preliminaries - Rust and Windows (Linux)