fn main() { let x: u32 = 4; let equal_to_x = move |z: u32| z == x; println!("x: {x}"); let y = 4; assert!(equal_to_x(y)); } #[cfg(test)] mod tests { #[test] #[ignore] fn closures_move_test() { let mut x = vec![1, 2, 3]; // 所有权转移到闭包内了 // let equal_to_x = move |z: Vec| z == x; let mut res: bool = false; let equal_to_x = |z: Vec| -> Vec { x.pop(); res = z == x; x }; let y = vec![1, 2, 3]; x = equal_to_x(y); println!("can't use x here {:?}", x); assert!(res); } #[test] fn closures_mut_test() { let mut x = vec![1, 2, 3]; let mut res: bool = false; let mut equal_to_x = |z: Vec| { x.pop(); res = z == x; }; let y = vec![1, 2, 3]; equal_to_x(y); println!("can't use x here {:?}", x); assert!(false); } }