Generic traits

Traits can also be generic. This is useful in scenarios where you want to implement a trait for a wide variety of types:

pub trait From<T> {
fn from(T) -> Self;
}

Two such examples are is the From<T> and Into<T> traits, which allow from conversion from a type to a type T and vice versa. Their use becomes prominent when these traits are used as trait bounds in function parameters. We'll see what trait bounds are and how they work in a moment. However, generic traits can get quite verbose when they are declared with three or four generic types. For those cases, we have associated type traits.

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

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