Rust中如何排序Vector?

banq發表於2022-09-21

Rust中如何排序數值列表?

fn main() {

   let mut vec = vec![1,23,42,23,45,223,211,122,233,799,123];
   vec.sort();
   println!("Sorted: {:?}", vec)
}


將浮點值向量排序為:

fn main() {
 let mut vec = vec![23.12, 3.44, 5.55, 34.90, 2.0];
 vec.sort_by(|x, y| x.partial_cmp(y).unwrap());
 println!("Sorted: {:?}", vec);

}

 

相關文章