January 2018
Beginner to intermediate
454 pages
10h 8m
English
Generics are a way to make a function or a type work for multiple types to avoid code duplication. Let's rewrite our max function to make it generic:
fn max<T: PartialOrd>(a: T, b: T) -> T { if a > b { a } else { b } }
The first thing to note is that there's a new part after the function name: this is where we declare the generic types. We declare a generic T type, : PartialOrd after it means that this T type must implement the PartialOrd trait. This is called a trait bound. We then use this T type for both of our parameters and the return type. Then, we see the same function body as the one from our non-generic function. We needed to add the trait bound because, by default, no operation is allowed on a generic type. The PartialOrd ...
Read now
Unlock full access