
Understand it
Trait bounds state exactly the capabilities the generic body needs, and the compiler generates a specialised copy per concrete type. This can avoid trait-object dispatch and enable inlining, but it does not make the body, allocations or copies free. Specialization can increase code size and compile time; a bound wider than necessary also narrows who can call you.
Generics let one implementation describe several concrete types without erasing what those types are. In the largest example, T is a placeholder chosen by the caller. The function does not need to know how to print or clone T; it only compares references, so PartialOrd is the capability that matters. A bound is a promise to the compiler about which operations the body may use, and it is a constraint on every caller. Adding Clone because it seems convenient would exclude useful types for no reason.
Read the signature from outside in: the input is a borrowed slice of T, and the output is a borrowed T from that slice. The function does not allocate or return a new owned value. It starts with the first item, walks the rest, and updates the best reference. This also reveals a precondition: indexing items[0] panics for an empty slice. A production API should state that precondition or return Option<&T> so emptiness is handled explicitly. Generic code still needs careful edge-case design.
At compilation, calls with concrete types can be specialized, so no runtime type switch is needed for a normal generic function. Try the function with integers and then with a comparable custom type. Make an empty input test and redesign the return type if that case should be safe. Finally, ask whether the chosen bound expresses precisely the behavior used. The aim is reusable code with an honest contract, not the longest possible list of trait bounds.
Read the code
fn largest<T: PartialOrd>(
items: &[T],
) -> &T {
let mut best = &items[0];
for item in items {
if item > best { best = item; }
}
best
}The bound names only what the body actually needs.
Practice starter
use std::fmt::Debug;
fn show<T: Clone + PartialOrd + Debug>(v: &T) {
println!("{v:?}");
}Try it
Return the larger owned input for any Ord type, without adding Copy or Clone bounds. This lab also calls the function with String. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
What is wrong with those bounds?
Reveal the answer
- Nothing — extra bounds cost the caller nothing
- Only Debug is used, so the rest exclude callers for no reason
- Debug should be dropped and the rest kept
Every unnecessary bound is a caller you turned away.
Go deeper
Common misconception: “Extra trait bounds are harmless because generics are free at runtime.”
Monomorphisation removes the runtime cost, but every bound is still a requirement on callers. show only uses Debug, so Clone and PartialOrd just turn away types that lack them.
- Read the bound:
largest<T: PartialOrd>borrows a slice and returns a reference into it.item > bestis the only operation it needs fromT. - Predict before checking: Predict what
largest(&[3, 8, 2])andlargest(&[1.5, 0.5])return. Then predict what an empty&[i32]does. - Compare with evidence: Run them:
8and1.5, both from one definition. The empty slice panics onitems[0], because that precondition is not in the signature.
Transfer challenge: Remove Clone and PartialOrd from show, keep Debug, and call it with a struct that derives only Debug. Then put Clone back and read the error at the call site.
Connections
- Lifetime Intuition: Why references stay valid
- Traits: Shared behavior and bounds
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Generics (courseId "ownership", lessonId "generics") Link: https://crabrix.com/learn/ownership/generics/ Goal: Concrete generic instantiations normally use static dispatch; measure code size, allocation and runtime cost. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/generics) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.