--- date: 20200917 id: 9062c9c0-b2d5-4719-b15b-6fe5122b6a9e title: Rust Generics --- # Function definitions We read the following definition as: the function largest is generic over some type T. This function has one parameter named list, which is a slice of values of type T. The largest function will return a reference to a value of the same type T. ``` rust fn largest(list: &[T]) -> &T { let mut largest = list[0]; for item in list { if item > largest { largest = item; } } largest } ``` # Struct definitions ``` rust struct Point { x: T, y: T, } fn main() { let integer = Point { x: 5, y: 10 }; let float = Point { x: 1.0, y: 4.0 }; } ``` A struct can also have multiple generic types: ``` rust struct Point { x: T, y: U, } fn main() { let both_integer = Point { x: 5, y: 10 }; let both_float = Point { x: 1.0, y: 4.0 }; let integer_and_float = Point { x: 5, y: 4.0 }; } ``` # Enum Definitions ``` rust #![allow(unused)] fn main() { enum Option { Some(T), None, } } ``` And again with multiple generics: ``` rust #![allow(unused)] fn main() { enum Result { Ok(T), Err(E), } } ``` # Method Definitions ``` rust struct Point { x: T, y: T, } impl Point { fn x(&self) -> &T { &self.x } fn y(&self) -> &T { &self.y } } fn main() { let p = Point { x: 5, y: 10 }; println!("p.x = {} and p.y={}", p.x(), p.y()); } ``` And again with multiple generics: ``` rust struct Point { x: T, y: U, } impl Point { fn mixup(self, other: Point) -> Point { Point { x: self.x, y: other.y, } } } fn main() { let p1 = Point { x: 5, y: 10.4 }; let p2 = Point { x: "Hello", y: 'c' }; let p3 = p1.mixup(p2); println!("p3.x = {}, p3.y = {}", p3.x, p3.y); } ``` # Performance of Code Using Generics The Rust compiler is very very clever and using generics has no performance penalty