Rust Mutability By Variable And Function

[Solved] Rust Mutability By Variable And Function | Perl - Code Explorer | yomemimo.com
Question : Rust mutability by variable and function

Answered by : nikola-anicic

fn add_and_print(x: &mut i32) { *x += 1; println!("The value of x in add_and_print is {}.", x); // mutable reference to x is dropped
}
fn main() { let mut x = 1; // Declare a mutable variable x add_and_print(&mut x); // Modify x in function x += 1; // Modify x again println!("The value of x in main is {}.", x);
}

Source : https://badboi.dev/rust/2020/07/17/cell-refcell.html | Last Update : Mon, 15 Aug 22

Answers related to rust mutability by variable and function

Code Explorer Popular Question For Perl