From 2c975a267caf4e8b73e45c037f576e8fc7619b6e Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 31 Jan 2026 13:01:21 +0100 Subject: [PATCH 1/2] generics: 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/generics/solution.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/generics/solution.md b/src/generics/solution.md index b4a4c92cd998..0aa71a3a3595 100644 --- a/src/generics/solution.md +++ b/src/generics/solution.md @@ -3,3 +3,24 @@ ```rust,editable {{#include exercise.rs:solution}} ``` + +- **Generic Function:** `min` is a generic function that takes two arguments of + type `T`. +- **Trait Bounds:** The syntax `` specifies that `T` must implement the + `Ord` trait. This is necessary because not all types can be compared (e.g., + floating-point numbers in Rust only implement `PartialOrd` due to `NaN`). +- **`Ord` Trait:** The `Ord` trait provides the `cmp` method, which compares two + values and returns an `Ordering`. +- **`Ordering` Enum:** The result of `cmp` is an enum with variants `Less`, + `Equal`, and `Greater`. We use `match` to handle these cases. + +
+ +- Mention that for floating point numbers, `f64` does not implement `Ord`, so + this function would not work for them. This is a deliberate design choice in + Rust to handle `NaN` correctly (NaN != NaN). To handle floats, one would + typically use `PartialOrd` or a wrapper type. +- Alternatively, `l <= r` works if we use `T: PartialOrd`. However, `Ord` is + stricter and guarantees a total order, which `cmp` relies on. + +
From ee4f804e5583b9fd13b0587409d89b3608d8a16c Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sat, 14 Feb 2026 10:39:45 +0100 Subject: [PATCH 2/2] Refine solution commentary for experienced programmers - Remove redundant or overly simplistic explanations. - Focus on Rust-specific idioms and design choices. - Clean up formatting and technical depth. --- src/generics/solution.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/generics/solution.md b/src/generics/solution.md index 0aa71a3a3595..7c35b1cbbc05 100644 --- a/src/generics/solution.md +++ b/src/generics/solution.md @@ -4,23 +4,21 @@ {{#include exercise.rs:solution}} ``` -- **Generic Function:** `min` is a generic function that takes two arguments of - type `T`. -- **Trait Bounds:** The syntax `` specifies that `T` must implement the - `Ord` trait. This is necessary because not all types can be compared (e.g., - floating-point numbers in Rust only implement `PartialOrd` due to `NaN`). -- **`Ord` Trait:** The `Ord` trait provides the `cmp` method, which compares two - values and returns an `Ordering`. -- **`Ordering` Enum:** The result of `cmp` is an enum with variants `Less`, - `Equal`, and `Greater`. We use `match` to handle these cases. +The solution uses trait bounds to constrain the generic type `T`: + +- **Trait Bounds:** The `` syntax requires that any type used with `min` + must implement the `Ord` trait, which guarantees a total ordering. +- **Comparison:** The `cmp` method returns an `Ordering` enum (`Less`, `Equal`, + or `Greater`), which we handle with pattern matching.
-- Mention that for floating point numbers, `f64` does not implement `Ord`, so - this function would not work for them. This is a deliberate design choice in - Rust to handle `NaN` correctly (NaN != NaN). To handle floats, one would - typically use `PartialOrd` or a wrapper type. -- Alternatively, `l <= r` works if we use `T: PartialOrd`. However, `Ord` is - stricter and guarantees a total order, which `cmp` relies on. +- **`Ord` vs. `PartialOrd`:** Rust distinguishes between types that have a total + order (`Ord`) and those that only have a partial order (`PartialOrd`). + Floating-point numbers (`f32`, `f64`) only implement `PartialOrd` because + `NaN` cannot be compared. Consequently, this `min` function cannot be called + with floats. +- **Standard Library:** In real-world code, one would typically use the built-in + `std::cmp::min` function or the `.min()` method available on many types.