Petite introduction aux traits
Les traits: pourquoi faire ? (10 minutes) Présentation de deux traits importants: Ord et Display.
code final:
fn affiche_max<T: std::fmt::Display + Ord>(v: &Vec<T>) {
let mut max = None;
for e in v {
if Some(e) > max {
max = Some(e);
}
}
if let Some(m) = max {
println!("le max est {}", m);
} else {
println!("pas de max");
}
}
fn main() {
let v = vec![1u8, 2, 3];
affiche_max(&v);
let v = vec![1u32, 2, 3];
affiche_max(&v);
}