I have the following code excerpt:
let mut counter: u32 = 0;
button.connect_clicked(move |_|
{
counter+=1;
flip_coin(&label)
}
);
I want to increment the counter variable when I click the GTK4 button. How can I achieve this, since in the code excerpt I provided , counter
is a captured variable in a Fn
closure.
For this, you need to use the Cell class and to clone your element for it to be used inside the closure of the button.
Small example :
let counter = Rc::new(Cell::new(0));
button.connect_clicked(clone!(@strong counter => move |_| {
counter.set(counter.get() + 1);
println!("{}", counter.get());
}));
More details here: Memory management with GTK4 and Rust