Examples · learn/10-generics · 160 of 339
largest
tin run examples/learn/10-generics/largest.tinfn largest<T>(xs: T[]): Option<T> {
if xs.len == 0 { return None }
var best = xs[0]
for x in xs {
if x > best { best = x }
}
Some(best)
}
fn main() {
print(largest([3, 9, 4]))
print(largest([2.5, -1.0]))
print(largest(["pear", "apple", "plum"])) // strings compare alphabetically
let nothing: Option<int> = largest([]) // here the type comes from the annotation
print(nothing)
}