where Clauses and Multiple Bounds
Keep complex signatures readable.
When Inline Bounds Get Crowded
Inline bounds like <T: Display + Clone, U: Debug + Default> become hard to read as they grow. Rust offers the where clause as a cleaner alternative.
It moves the constraints below the signature, keeping the parameter list short.
Basic where Clause
A where clause sits between the return type and the body. Each line lists a type and its bounds.
These two signatures mean exactly the same thing; the where form just reads better.
use std::fmt::Display;
fn show<T>(value: T)
where
T: Display,
{
println!("{}", value);
}All lessons in this course
- Generic Functions
- Generic Structs and Enums
- Trait Bounds
- where Clauses and Multiple Bounds