rust实现求数组切片最大元素

涉及到Rust的泛型,特性,数组切片等知识,是个不错的例子

fn largest<T>(list: &[T]) -> &T
    where T: PartialOrd
{
    let mut largest = &list[0];
    for item in list.iter() {
        if item > largest {
            largest = item
        }
    }
    largest
}
fn main() {
    let nums = vec![1, 8, 23, 12];
    let chars = vec!['a', 'c', 'g', 'f'];
    let res1 = largest(&nums);
    let res2 = largest(&chars);
    println!("{}, {}", res1, res2);
}

你可能感兴趣的:(Rust,rust,开发语言,后端)