Files
learn-rust/lessons/0001-diagnostic-ch1-9.html

240 lines
17 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Diagnostic: Rust Book ch. 1–9</title>
<link rel="stylesheet" href="../assets/style.css" />
<script src="../assets/quiz.js" defer></script>
</head>
<body>
<h1>Diagnostic: what's still there from ch. 1–9</h1>
<p class="subtitle">Lesson 0001 · Rust Book chapters 1–9 · no new material, pure retrieval</p>
<p>Your <code>learn-*</code> folders prove you worked through variables, functions, control flow, ownership, borrowing, slices, structs, enums, modules, collections, and error handling. This lesson does not re-teach any of it. It tests what is still <em>retrievable</em> — that is the only kind of "knowing" that helps in an interview or on the job.</p>
<div class="callout">
<strong>How to take this.</strong> For each question, think of your answer <em>before</em> revealing it. Grading yourself honestly matters more than "winning" — this diagnostic decides which lessons get built next. Multiple-choice ones grade themselves; short-answer ones ask you to self-grade after reading the answer.
</div>
<div class="q" data-type="recall" data-topic="Variables">
<p class="topic">Variables — ch03.1</p>
<p class="prompt">This code:</p>
<pre><code>let x = 5;
x = 6;
println!("{x}");</code></pre>
<p class="prompt">Does it compile? If not, what's the fix?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">No — <code>cannot assign twice to immutable variable</code>. Rust variables are immutable by default; fix by declaring <code>let mut x = 5;</code>.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html">3.1 Variables and Mutability</a></p>
</div>
<div class="q" data-type="recall" data-topic="Ownership">
<p class="topic">Ownership — ch04.1</p>
<p class="prompt">This code:</p>
<pre><code>let s1 = String::from("hello");
let s2 = s1;
println!("{s1}");</code></pre>
<p class="prompt">Does it compile? Why or why not?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">No. <code>String</code> owns heap data and is not <code>Copy</code>, so <code>let s2 = s1;</code> <em>moves</em> ownership from <code>s1</code> to <code>s2</code>. Using <code>s1</code> afterward is a compile error: "borrow of moved value."</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html">4.1 What Is Ownership?</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Structs">
<p class="topic">Structs — ch05.3</p>
<p class="prompt">Given <code>struct Rectangle { width: u32, height: u32 }</code>, where do you define a method like <code>fn area(&self) -> u32</code>?</p>
<div class="options">
<button class="opt" data-correct="true">Inside an impl block.</button>
<button class="opt" data-correct="false">Inside the struct body.</button>
<button class="opt" data-correct="false">Inside the main function.</button>
<button class="opt" data-correct="false">Inside a trait block.</button>
</div>
<div class="explain hidden">Methods are defined in a separate <code>impl Rectangle { ... }</code> block, not inside the struct definition itself.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch05-03-method-syntax.html">5.3 Method Syntax</a></p>
</div>
<div class="q" data-type="recall" data-topic="Error Handling">
<p class="topic">Error Handling — ch09</p>
<p class="prompt">What's the real difference between calling <code>panic!</code> and returning <code>Err(...)</code> from a function whose signature returns <code>Result</code>?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden"><code>panic!</code> unwinds and stops the program right there — unrecoverable. Returning <code>Err</code> hands the failure back to the caller, who decides what to do — recoverable. Libraries should almost always prefer <code>Result</code>.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch09-00-error-handling.html">9. Error Handling</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Data Types">
<p class="topic">Data Types — ch03.2</p>
<p class="prompt">With no suffix and no other constraint, what type does Rust infer for <code>let x = 5;</code>?</p>
<div class="options">
<button class="opt" data-correct="true">i32</button>
<button class="opt" data-correct="false">i64</button>
<button class="opt" data-correct="false">u32</button>
<button class="opt" data-correct="false">f64</button>
</div>
<div class="explain hidden"><code>i32</code> is Rust's default integer type — fastest on most platforms, per the book.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch03-02-data-types.html">3.2 Data Types</a></p>
</div>
<div class="q" data-type="mcq" data-topic="References & Borrowing">
<p class="topic">References &amp; Borrowing — ch04.2</p>
<p class="prompt">Inside <code>fn calculate_length(s: &String) -> usize</code>, can you modify the <code>String</code> through <code>s</code>?</p>
<div class="options">
<button class="opt" data-correct="true">No, references are read only.</button>
<button class="opt" data-correct="false">Yes, references allow all edits.</button>
<button class="opt" data-correct="false">No, only String owns access.</button>
<button class="opt" data-correct="false">Yes, but only for numbers.</button>
</div>
<div class="explain hidden">References are immutable by default. Modifying through a reference needs <code>&mut String</code>, and the caller must pass <code>&mut s</code>.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html">4.2 References and Borrowing</a></p>
</div>
<div class="q" data-type="recall" data-topic="Enums & Pattern Matching">
<p class="topic">Enums &amp; Pattern Matching — ch06.2</p>
<p class="prompt">Why must a <code>match</code> expression cover every variant of an enum?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">The compiler enforces exhaustiveness — every possible value must be handled, or it's a compile error. Use <code>_</code> as a catch-all arm when you don't care about the rest.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch06-02-match.html">6.2 The match Control Flow Construct</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Common Collections">
<p class="topic">Common Collections — ch08.1</p>
<p class="prompt">Which type owns and can grow a heap-allocated list of values of the same type?</p>
<div class="options">
<button class="opt" data-correct="true">Vec, a growable vector.</button>
<button class="opt" data-correct="false">Array, a fixed list.</button>
<button class="opt" data-correct="false">Slice, a borrowed view.</button>
<button class="opt" data-correct="false">Tuple, a fixed grouping.</button>
</div>
<div class="explain hidden"><code>Vec&lt;T&gt;</code> owns its elements on the heap and grows/shrinks at runtime. Arrays are fixed-size and stack-allocated; slices only borrow.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch08-01-vectors.html">8.1 Storing Lists of Values with Vectors</a></p>
</div>
<div class="q" data-type="recall" data-topic="Functions">
<p class="topic">Functions — ch03.3</p>
<p class="prompt">Does the last expression in a function body need a <code>return</code> keyword to become the return value? What one character must you drop from that line for it to count as an expression, not a statement?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">No <code>return</code> needed. Drop the trailing semicolon — a line ending in <code>;</code> is a statement (no value); without it, it's an expression whose value becomes the function's return value.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch03-03-how-functions-work.html">3.3 Functions</a></p>
</div>
<div class="q" data-type="recall" data-topic="Slices">
<p class="topic">Slices — ch04.3</p>
<pre><code>let s = String::from("hello world");
let hello = &s[0..5];</code></pre>
<p class="prompt">What Rust type is <code>hello</code>? A slice stores two things internally — what are they?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden"><code>hello</code> is a <code>&str</code> (string slice). Internally a slice is a pointer into the buffer plus a length — it never owns the data.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch04-03-slices.html">4.3 The Slice Type</a></p>
</div>
<div class="q" data-type="recall" data-topic="Modules & Paths">
<p class="topic">Modules &amp; Paths — ch07</p>
<p class="prompt">In your <code>restauran</code> project you called <code>crate::front_of_house::hosting::add_to_waitlist()</code>. What does the <code>pub</code> keyword do to <code>hosting</code> and to <code>add_to_waitlist</code>? What breaks if you remove it?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden"><code>pub</code> makes an item visible outside its parent module. Everything is private by default, so removing <code>pub</code> from either the module or the function makes it inaccessible from <code>eat_at_restaurant</code>'s call site — a compile error ("module/function is private").</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html">7. Packages, Crates, and Modules</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Control Flow">
<p class="topic">Control Flow — ch03.5</p>
<p class="prompt">Can <code>if</code> sit on the right side of a <code>let</code> to assign a value, like a ternary?</p>
<div class="options">
<button class="opt" data-correct="true">Yes, if arms must match.</button>
<button class="opt" data-correct="false">No, if never returns values.</button>
<button class="opt" data-correct="false">Yes, but types can differ.</button>
<button class="opt" data-correct="false">No, only loops return values.</button>
</div>
<div class="explain hidden">Yes — <code>let x = if cond { 5 } else { 6 };</code> works, but every arm must produce the <em>same</em> type, since <code>x</code> needs one fixed type at compile time.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html">3.5 Control Flow</a></p>
</div>
<div class="q" data-type="recall" data-topic="Ownership">
<p class="topic">Ownership — ch04.1</p>
<p class="prompt">Name the trait that lets simple stack-only types like <code>i32</code> get duplicated instead of moved on assignment.</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">The <code>Copy</code> trait. Types with a known, fixed size that live entirely on the stack (integers, bools, chars, tuples of Copy types, …) can implement it, so assignment copies instead of moving.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html">4.1 What Is Ownership? — Stack-Only Data: Copy</a></p>
</div>
<div class="q" data-type="recall" data-topic="Structs">
<p class="topic">Structs — ch05.3</p>
<p class="prompt">Why do most struct methods take <code>&self</code> (borrowed) instead of <code>self</code> (owned)?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">Taking <code>self</code> by value would move the instance into the method, consuming it — the caller couldn't use it afterward. <code>&self</code> borrows it, so the instance survives the call.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch05-03-method-syntax.html">5.3 Method Syntax</a></p>
</div>
<div class="q" data-type="recall" data-topic="Common Collections">
<p class="topic">Common Collections — ch08.3</p>
<p class="prompt">From your own <code>learn-challenges</code> code:</p>
<pre><code>let x = count.entry(v).or_insert(0);
*x += 1;</code></pre>
<p class="prompt">What does <code>.entry(v)</code> return, and what does <code>.or_insert(0)</code> do whether <code>v</code> is a new key or an existing one?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden"><code>.entry(v)</code> returns an <code>Entry</code> enum for that key. <code>.or_insert(0)</code> inserts <code>0</code> only if the key is missing, and in both cases returns a mutable reference to the value — so <code>*x += 1</code> works whether it's brand-new or already there.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html">8.3 Storing Keys with Associated Values in Hash Maps</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Enums & Pattern Matching">
<p class="topic">Enums &amp; Pattern Matching — ch06.3</p>
<p class="prompt">What's the shorthand for a <code>match</code> that only cares about one pattern and ignores every other case?</p>
<div class="options">
<button class="opt" data-correct="true">Use an if let.</button>
<button class="opt" data-correct="false">Use a for loop.</button>
<button class="opt" data-correct="false">Use a while let.</button>
<button class="opt" data-correct="false">Use the unwrap method.</button>
</div>
<div class="explain hidden"><code>if let Some(x) = opt { ... }</code> handles one pattern concisely without a full exhaustive <code>match</code>. <code>while let</code> is the loop version, not this case.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch06-03-if-let.html">6.3 Concise Control Flow with if let</a></p>
</div>
<div class="q" data-type="mcq" data-topic="Error Handling">
<p class="topic">Error Handling — ch09.2</p>
<p class="prompt">From your own <code>learn-panic</code> code:</p>
<pre><code>let mut username_file = File::open("hello.txt")?;</code></pre>
<p class="prompt">What does the <code>?</code> do when <code>File::open</code> returns <code>Err</code>?</p>
<div class="options">
<button class="opt" data-correct="true">It returns Err early.</button>
<button class="opt" data-correct="false">It panics the program.</button>
<button class="opt" data-correct="false">It retries the call.</button>
<button class="opt" data-correct="false">It ignores the error.</button>
</div>
<div class="explain hidden"><code>?</code> propagates the error immediately: the enclosing function returns that <code>Err</code> right away instead of continuing — the function's return type must be a compatible <code>Result</code>.</div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch09-02-recoverable-errors-with-result.html">9.2 Recoverable Errors with Result</a></p>
</div>
<div class="q" data-type="recall" data-topic="References & Borrowing">
<p class="topic">References &amp; Borrowing — ch04.2</p>
<p class="prompt">True or false: at any single point, Rust allows either one mutable reference OR any number of immutable references to the same data, never a mix. Why does this rule exist?</p>
<button class="reveal-btn">Show answer</button>
<div class="answer hidden">True. It's enforced at compile time by the borrow checker, and it exists to rule out data races and "reader sees a half-written value" bugs without needing a garbage collector or runtime lock.</div>
<div class="grade hidden"><button data-grade="hit">Got it</button><button data-grade="miss">Missed it</button></div>
<p class="cite">Book: <a href="https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html">4.2 References and Borrowing</a></p>
</div>
<div id="summary">
<h2>Results</h2>
<div id="summary-body">No questions answered yet.</div>
<p id="summary-total"></p>
<button id="report-btn" disabled>Copy report</button>
<pre id="report-output" class="hidden"></pre>
<p class="cite">Copying the report and pasting it into the chat is the fastest way to tell the agent what to build next.</p>
</div>
<footer>
<p><strong>Primary source:</strong> <a href="https://doc.rust-lang.org/stable/book/">The Rust Programming Language</a>, chapters 1–9, by Klabnik, Nichols &amp; Krycho.</p>
<p><strong>Next:</strong> <a href="0002-write-a-cli-from-blank.html">0002 — Write a CLI from a blank file</a> · <strong>Reference:</strong> <a href="../reference/rust-syntax.html">Rust syntax reference (ch. 1–9)</a></p>
<p>Stuck on anything above, or a question phrased badly? Ask the agent — that's what it's there for. Paste your weak topics from the report and the next lesson will target exactly those.</p>
</footer>
</body>
</html>