Pass by reference to change external variable - Increment in a function
examples/functions/increment/src/main.rs
fn main() { let mut n = 1; println!("before: {n}"); increment(&mut n); println!("after: {n}"); } fn increment(val: &mut i32) { println!("start: {val}"); *val += 1; println!("end: {val}"); }
before: 1 start: 1 end: 2 after: 2