Trait bounds on types

We can specify trait bounds on types too:

// trait_bounds_types.rs

use std::fmt::Display;

struct Foo<T: Display> {
bar: T
}

// or

struct Bar<F> where F: Display {
inner: F
}

fn main() {}

However, trait bounds on types are discouraged as it places restrictions on types themselves. Generally, we want types to be as generic as possible, allowing us to create instances using any type, and instead place restrictions on their behavior using traits bounds in functions or methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset