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
作者:天使不设防
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.