rust 泛型

 1 fn max<T: std::cmp::PartialOrd >(nn: &Vec<T>) -> &T {
 2     let mut f = &nn[0];
 3     for i in nn{
 4         if i > f {
 5             f = i;
 6         }
 7     }
 8     f
 9 }
10 fn main(){
11     let v = vec![11,23,13,1,2,4,678,3,215,61];
12     let k = vec![11.2,23.3,13.12,61.98];
13     let m = vec!["lilei","zhangs","wangw","liulu"];
14     let ret = max(&v);
15     let rrt = max(&k);
16     let rwt = max(&m);
17     println!("max int is: {} ",ret);
18     println!("max float is: {} ",rrt);
19     println!("max &str is: {}", rwt);
20 }

## output
max int is: 678
max float is: 61.98
max &str is: zhangs

posted @ 2021-12-10 16:07  天使不设防  阅读(39)  评论(0编辑  收藏  举报