Trait bounds
A trait bound restricts a generic type to implementers of a trait. T: Display means the function or impl is only valid when T can be formatted.
Detailed Explanation
A generic function without bounds can only use operations that exist on every type: move, drop, size-of. The moment you write println!("{item}") you need Display. The compiler's question is T: Display, and you answer it in the signature: fn show<T: Display>(item: T). Callers with a T that does not implement Display get a compile error at the call site, not inside show.
Bounds can be written inline (T: Clone + Debug) or in a where clause (where T: Clone, T: Debug). where is more readable once you have several parameters, associated-type bounds (T::Item: Debug), or for<'a> higher-ranked bounds. Both forms mean the same thing.
Common bounds from the standard library: Clone when you need a second owned value, Copy when the type is a trivial memcpy, Debug for format strings with {:?}, Send and Sync when a value crosses threads, Eq + Hash for HashMap keys, Ord for sorting. ?Sized is the unusual one: it relaxes the implicit T: Sized so the parameter can be a trait object or a slice.
Bounds on impl blocks restrict the whole block: impl<T: Display> Stack<T>. Methods inside can assume Display. Types that do not implement Display still use Stack<T>; they just do not see those methods. That is how Vec<T> can offer fn sort(&mut self) only when T: Ord.
A bound is not a runtime check. It is erased after monomorphisation. Each concrete T gets its own copy of the function, with the trait methods inlined as direct calls. That is why generic bounds are the fast path and dyn Trait is the other path.
When a bound grows into T: Clone + Debug + Display + Send + Sync + 'static, stop and name a trait that inherits those: trait Wire: Clone + Debug + Send + Sync + 'static {}. Or take a concrete type. A six-trait bound is a sign the function is doing too much or the abstraction is wrong.
When you write a bound: the body uses a trait method or stores the value in a container that requires one. When you do not: the function only moves or borrows the value, and then no bound is needed.
Code Examples
use std::fmt::Display;
fn show<T: Display>(item: T) {
println!("{item}");
}
fn both<T, U>(a: T, b: U) -> String
where
T: Display,
U: Display,
{
format!("{a} {b}")
}
fn main() {
show(17); // prints: 17
println!("{}", both("port", 8080)); // prints: port 8080
}Common Gotchas
Missing bound is an error inside the generic, not a runtime miss
Add `T: Display` or stop using `Display` methods. Do not `unwrap` your way around a missing bound; there is no runtime fallback.
`+` combines bounds
`T: Clone + Debug` is both. There is no `or` of two traits on one type parameter; use an enum if a value is one of two traits.
Long bound lists are a design smell
Name a supertrait, or take `dyn Trait`, or split the function. Callers should not have to satisfy six traits for a helper.
`where` can bound associated types
`where T: Iterator, T::Item: Debug` is how you talk about the item, not only `T`.