Gtk-rs use of rc without having to clone variable

⚓ rust    📅 2025-05-29    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 31 days ago. The information described in this article may have changed.

I have this variable over here:

let value = Rc::new(Mutex::new(0u8));

inside the call back I have this:

button.connect_clicked(move |_|
{
                let value = Rc::clone(&value);
                let mut counter = value.lock().unwrap();
                *counter += 1;
});

Then outside of the callback I have this:

        {
            let widget_box = gtk::Box::new(Orientation::Vertical, 1);
            let value = Rc::clone(&value);
            let counter = value.lock().unwrap();
            let label = Label::new(Some(&format!("No of Jobs: {}", counter)));
            widget_box.add(&label);
            mainbox.add(&widget_box);
        }

and I get this error:

value borrowed here after move

Whilst I can clone the variable outside of the closure but is there another alternative without having to create another variable and clone the main variable outside the closure?

3 posts - 3 participants

Read full topic

🏷️ rust_feed