From be6cac83549c68a8af1e9fb253d988914040ee21 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:05:14 +0100 Subject: [PATCH 1/2] pattern-matching: add explanatory commentary to solution This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts. --- src/pattern-matching/solution.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/pattern-matching/solution.md b/src/pattern-matching/solution.md index b4a4c92cd998..545a310ffa6d 100644 --- a/src/pattern-matching/solution.md +++ b/src/pattern-matching/solution.md @@ -3,3 +3,25 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +- **Pattern Matching:** We use `match` to handle the different variants of the + `Expression` enum. This ensures we cover all possible cases. +- **Destructuring:** The pattern `Expression::Op { op, left, right }` destructures + the `Op` variant, giving us access to its inner fields. +- **Recursion:** Since `Expression` is a recursive data structure, `eval` is a + recursive function. We call `eval(*left)` and `eval(*right)` to compute the + values of the sub-expressions. +- **Smart Pointers (`Box`):** The `left` and `right` fields are of type + `Box`. `Box` puts the value on the heap. The `*` operator + dereferences the box, moving the inner `Expression` out so it can be passed by + value to the recursive `eval` calls. + +
+ +- Mention that `Box` is necessary because `Expression` has infinite size otherwise + (it contains itself). `Box` provides a fixed size (pointer size) for the recursive + fields. +- Discuss integer division behavior (truncation) for `Operation::Div` since we are + using integers. + +
From 1ccfa1c8b1f8f6f1504ef680a9f0a6bf696fcc25 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:07:03 +0100 Subject: [PATCH 2/2] pattern-matching: add explanatory commentary to solution This commentary, written by Gemini, focuses on aspects of the solution that differ from the baseline languages (C/Java/Python), highlighting Rust-specific idioms and concepts. --- src/pattern-matching/solution.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/pattern-matching/solution.md b/src/pattern-matching/solution.md index 545a310ffa6d..494554c9a394 100644 --- a/src/pattern-matching/solution.md +++ b/src/pattern-matching/solution.md @@ -6,8 +6,8 @@ - **Pattern Matching:** We use `match` to handle the different variants of the `Expression` enum. This ensures we cover all possible cases. -- **Destructuring:** The pattern `Expression::Op { op, left, right }` destructures - the `Op` variant, giving us access to its inner fields. +- **Destructuring:** The pattern `Expression::Op { op, left, right }` + destructures the `Op` variant, giving us access to its inner fields. - **Recursion:** Since `Expression` is a recursive data structure, `eval` is a recursive function. We call `eval(*left)` and `eval(*right)` to compute the values of the sub-expressions. @@ -18,10 +18,10 @@
-- Mention that `Box` is necessary because `Expression` has infinite size otherwise - (it contains itself). `Box` provides a fixed size (pointer size) for the recursive - fields. -- Discuss integer division behavior (truncation) for `Operation::Div` since we are - using integers. +- Mention that `Box` is necessary because `Expression` has infinite size + otherwise (it contains itself). `Box` provides a fixed size (pointer size) for + the recursive fields. +- Discuss integer division behavior (truncation) for `Operation::Div` since we + are using integers.