Challenge 3: Why if (x = 5) Compiles, and Why Rust Can't Have This Bug — Possible Solution ==================================================================== `if (x = 5)` compiles cleanly in C because of two facts combining: (1) `=` is an assignment EXPRESSION in C, not just a statement -- an assignment evaluates to the value that was assigned, so `x = 5` is itself a valid expression whose value is 5; and (2) per the chapter, `if` accepts ANY expression as its condition, evaluated as "is this nonzero" -- there is no requirement that the condition be a genuine comparison or a boolean at all. What it actually does: `x = 5` assigns 5 to x (a real, permanent side effect the programmer likely didn't intend), and the `if` then evaluates the RESULT of that assignment (5) for truthiness. Since 5 is nonzero, the condition is true -- always, regardless of whatever value x held before. The programmer almost certainly meant `if (x == 5)` (a comparison), but wrote `=` (assignment) by mistake, and C's rules allow the mistake to compile as a different, silently valid program instead of catching it as an error. Why the equivalent mistake isn't possible in Rust: Rust's assignment (`x = 5`) evaluates to the unit type `()`, not to the assigned value -- so even if someone wrote `if x = 5 { ... }`, the condition wouldn't be usable as a boolean at all (`()` isn't a `bool`), and Rust's compiler requires an `if` condition to be a genuine `bool` expression, full stop. The combination that makes C's version dangerous -- assignment producing a usable value, AND any nonzero value being an acceptable condition -- simply doesn't exist in Rust; either piece alone would be enough to block it, and Rust removes both. WHY THIS WORKS AS AN ANSWER ------------------------------ This identifies the TWO separate C language rules (assignment as a value-producing expression, plus any-nonzero-condition) that combine to allow the bug, rather than treating it as one vague quirk, and explains precisely which of Rust's design choices removes each half of that combination.