liveBook · Manning

Trait bound

As explained in Learn Rust in a Month of Lunches

A trait bound is a Rust constraint requiring a type to implement one or more specified traits before it can be used in a bounded context.

A trait bound says what a type must be able to do before it can be used somewhere. Think of it as a qualification: a function need not care which type it receives, as long as that type implements the required trait. A trait can even have no methods and still serve as a qualification. section 7.1.3

Require two abilities

`fn print_maximum<T: PartialOrd + Display>(one: T, two: T)` accepts two values of the same type `T`. `PartialOrd` permits the comparison; `Display` permits printing the result with `{}`. The `+` means both traits are required. The bounds can also follow `where` instead of appearing beside `T`. section 12.2.1 section 7.1.3

A bound can also appear on a trait: `trait MonsterBehavior: Debug` requires every type implementing `MonsterBehavior` to implement `Debug`. That lets a default method print `self` with `{:?}`, even though the trait does not know the concrete type. A method can separately accept `T: MonsterBehavior` and call the abilities that trait provides. section 7.1.2

use std::fmt::Display;
 
fn print_maximum<T: PartialOrd + Display>(one: T, two: T) {
    let higher = if one > two { one } else { two };
    println!("{higher} is higher.");
}
 
fn main() {
    print_maximum(8, 10);
}

The complete `print_maximum` example shows the shared type `T`, its two bounds, and a call using numbers. section 12.2.1

Compiler errors can reveal an important distinction. Replacing each `T` in that comparison with a separate `impl PartialOrd + Display` does not say the arguments have the same type, so comparing them fails. Keep the named `T` when arguments must share a type; use `impl Trait` when that shared-type relationship is not needed. section 12.2.1

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains single and multiple bounds through working functions, trait definitions, and a failed impl Trait comparison.
Examples
Uses combat abilities, a number-comparison function, and a compiler error from comparing separately bounded arguments.
What it assumes
Builds on traits, structs, and functions while introducing bounds through a plain-language qualifications analogy.
What you can do after
Build it from scratch: write bounds that permit required operations and choose a named generic when arguments must share a type.
How it connects
Connects trait bounds to generics, Debug, Display, PartialOrd, impl Trait, and closure traits.

Also explained in

Compare how 2 books explain trait bound →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.