When do I need to make a closure variable mutable? [duplicate]
问题 This question already has an answer here : When should I add mut to closures? (1 answer) Closed last month . Why do I need to make this closure variable mutable? The closure isn't returning anything, therefore nothing is being stored in the closure variable. This closure is only capturing a value from the environment and incrementing it. fn main() { let mut x = 1; let mut y = || x = x + 1; y(); println!("{}", x); } 回答1: Essentially, this is a consequence of one of Rust's soundness rules – you