// The compiler can't assume anything about T, must give // the call a Haskell like constraint fn largest(list: &[T]) -> &T { let mut largest = &list[0]; for item in list { if item > largest { largest = item; } } largest } struct Point { x: T, y: T, } impl Point { fn x(&self) -> &T { &self.x } } // Can have non-generic implementations impl Point { fn distance_from_origin(&self) -> f32 { (self.x.powi(2) + self.y.powi(2)).sqrt() } } fn main() { let number_list = vec![34, 50, 25, 100, 65]; let result = largest(&number_list); println!("The largest number is {result}"); let char_list = vec!['y', 'm', 'a', 'q']; let result = largest(&char_list); println!("The largest char is {result}"); let integer = Point { x: 5, y: 10 }; let float = Point { x: 1.0, y: 4.0 }; println!("integer.x = {}", integer.x()); }