Rust Generics Bounded generic types


// Only accept T and U generic types that also implement Debug
fn print_objects<T: Debug, U: Debug>(a: T, b: U) {
    println!("A: {:?} B: {:?}", a, b);

print_objects(13, 44);
// or annotated explicitly
print_objects::<usize, u16>(13, 44);

The bounds must cover all uses of the type. Addition is done by the std::ops::Add trait, which has input and output parameters itself. where T: std::ops::Add<u32,Output=U> states that it's possible to Add T to u32, and this addition has to produce type U.

fn try_add_one<T, U>(input_value: T) -> Result<U, String> 
    where T: std::ops::Add<u32,Output=U> 
    return Ok(input_value + 1);

Sized bound is implied by default. ?Sized bound allows unsized types as well.