41 lines
876 B
Rust
41 lines
876 B
Rust
|
mod lib;
|
||
|
mod point;
|
||
|
|
||
|
use point::{Point, DoubleGenericPoint};
|
||
|
|
||
|
// Works on types implementing both the PartialOrd and Copy traits
|
||
|
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {
|
||
|
let mut largest = list[0];
|
||
|
|
||
|
for &item in list.iter() {
|
||
|
if item > largest {
|
||
|
largest = item;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
largest
|
||
|
}
|
||
|
|
||
|
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 p = Point { x: 5, y: 10 };
|
||
|
|
||
|
println!("p.x = {}", p.x());
|
||
|
|
||
|
let p1 = DoubleGenericPoint { x: 5, y: 10.4 };
|
||
|
let p2 = DoubleGenericPoint { x: "Hello", y: 'c'};
|
||
|
|
||
|
let p3 = p1.mixup(p2);
|
||
|
|
||
|
println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
|
||
|
}
|