Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/generics/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,22 @@
```rust,editable
{{#include exercise.rs:solution}}
```

The solution uses trait bounds to constrain the generic type `T`:

- **Trait Bounds:** The `<T: Ord>` 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.

<details>

- **`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.

</details>