Skip to main content
Be Bitwise
RustTraits & Generics

Trait Bounds

Trait bounds constrain generic types to those implementing specific traits. They're how you tell the compiler what a generic type can actually do.

Basic Bounds

rust
fn print_info<T: Display + Debug>(item: T) {
println!("Display: {}", item);
println!("Debug: {:?}", item);
}

T: Display + Debug means T must implement both Display and Debug.

impl Trait Syntax

A more concise syntax for simple cases:

rust
fn notify(item: &impl Summary) {
println!("{}", item.summarize());
}
// Equivalent to:
fn notify<T: Summary>(item: &T) {
println!("{}", item.summarize());
}

Return impl Trait

Return a type that implements a trait without specifying the concrete type:

rust
fn create_summarizable() -> impl Summary {
Article {
title: String::from("Test"),
author: String::from("Author"),
content: String::from("Content"),
}
}
Key Concept

impl Trait in return position earns its keep when the concrete type is gnarly or an implementation detail. The caller only sees that it implements Summary — whether it's an Article underneath stays hidden. Encapsulation by another name.

Conditional Method Implementation

Implement methods only when bounds are satisfied:

rust
struct Wrapper<T> {
value: T,
}
impl<T: Display> Wrapper<T> {
fn show(&self) {
println!("{}", self.value);
}
}
impl<T: Display + PartialOrd> Wrapper<T> {
fn show_if_positive(&self)
where
T: Default,
{
if self.value > T::default() {
println!("{}", self.value);
}
}
}

Blanket Implementations

Implement a trait for all types satisfying a bound:

rust
trait Printable {
fn print(&self);
}
// Implement Printable for ALL types that implement Display
impl<T: Display> Printable for T {
fn print(&self) {
println!("{}", self);
}
}
42.print(); // works because i32: Display
"hello".print(); // works because &str: Display

The standard library lives on this trick. ToString, for instance, is implemented for every type that implements Display.

This program puts the three ideas side by side: a function with two bounds, an impl Trait return and a blanket impl.

Try itDelete the impl Display for Article block and run: see which bound the compiler names, and where.
Rust (1.40.0)

Associated Types vs Generics

Sometimes associated types are cleaner than generics:

rust
// With generic (multiple implementations possible per type):
trait Container<T> {
fn get(&self) -> &T;
}
// With associated type (one implementation per type):
trait Container {
type Item;
fn get(&self) -> &Self::Item;
}
impl Container for Vec<i32> {
type Item = i32;
fn get(&self) -> &i32 {
&self[0]
}
}

The Iterator trait uses an associated type: type Item. Each iterator has exactly one element type — pinned, not chosen by the caller.

The practical difference shows up at the call site. A generic trait lets one type implement it several times, so the caller sometimes has to say which one. An associated type allows one implementation, so the compiler can work the element type out on its own:

That's why bounds on iterators read I: Iterator<Item = i32>. You're constraining the one Item the iterator already has, rather than choosing a version of the trait.