xxxxxxxxxx
use std::cell::Cell;
#[derive(Debug)]
struct XStruct<'a> {
x: &'a Cell<i32>,
}
fn add_and_print(x_struct: &XStruct) {
let x = x_struct.x.get();
x_struct.x.set(x + 1);
println!("The value of x_struct in add_and_print is {:?}.", x_struct);
}
fn main() {
let cell_x = Cell::new(1);
let x_struct = XStruct { x: &cell_x };
add_and_print(&x_struct);
let x = cell_x.get();
cell_x.set(x + 1);
println!("Final value of x_struct is {:?}.", x_struct);
println!("Final value of x is {:?}.", cell_x);
}