keyword

Rust where: express generic constraints clearly

A where clause lists requirements on generic types or lifetimes. It keeps complex bounds separate from a function’s parameter list. Expected output (not executed): Value: 8 Value: Tea Why: Both the integer and string reference support Display. Common mistake: A where clause is checked as a type constraint, not evaluated as a runtime if condition.

Formal Syntax & Structure

rust syntaxrust
fn name<T>(value: T) where T: Trait {}

Working Example

Example Snippetrust
use std::fmt::Display;fn show<T>(value: T) where T: Display {    println!("Value: {value}");}
fn main() {    show(8);    show("Tea");}
Rust Reference