文章目录
rust
/*fn swap<'a>(a: &'a mut String, b:&'a mut String){
let tmp = *a;
*a = *b;
*b = tmp;
}
let mut a = "aaa".to_string();
let mut b = "bbb".to_string();
swap(&mut a, &mut b);*/
let mut a = 111;
let mut b = 222;
fn swap<'a>(a: &'a mut i32, b:&'a mut i32){
let tmp = *a;
*a = *b;
*b = tmp;
}
/*
fn swap(mut a: i32, mut b: i32){
let tmp = a;
a = b;
b = tmp;
}
swap( a, b);
*/
swap(&mut a, &mut b);
最高效方式
rust
let mut a = "aaa".to_string();
let mut b = "bbb".to_string();
std::mem::swap(&mut a, &mut b);