-1

I want to get an input from an entry on a button click and display that information when another button is clicked. This gives me an error because the closure takes ownership of my firstname variable, in which I want to store the information.

How do I get the information out of the entry and reuse it?

// import gtk libs
extern crate gio;
extern crate gtk;

// declare use of gtk
use gtk::prelude::*;

fn main() {
    let mut firstname = String::new();

    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }
    let glade_src = include_str!("builder.glade");
    let builder = gtk::Builder::new_from_string(glade_src);

    let window: gtk::Window = builder.get_object("window1").unwrap();
    let buttonSubmit: gtk::Button = builder.get_object("buttonSubmit").unwrap();
    let buttonShow: gtk::Button = builder.get_object("buttonShow").unwrap();
    let entryFirstname: gtk::Entry = builder.get_object("entryFirstname").unwrap();

    // get information from entry
    buttonSubmit.connect_clicked(move |_| {
        firstname = entryFirstname.get_buffer().get_text();
    });

    // output information
    let firstname_clone = firstname.clone();
    buttonShow.connect_clicked(move |_| {
        println!("Firstname: {}", firstname_clone);
    });

    window.show_all();

    gtk::main();
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
jonryan
  • 11
  • 2
  • This might help you https://stackoverflow.com/questions/30559073/cannot-borrow-captured-outer-variable-in-an-fn-closure-as-mutable – Andra Oct 28 '19 at 01:50
  • Your question may be answered by the answers of [How to set a variable inside a gtk-rs closure?](https://stackoverflow.com/q/45702112/155423); [HOWTO: Idiomatic Rust for callbacks with gtk (rust-gnome)](https://stackoverflow.com/q/31966497/155423); [Alternative way to handle GTK+ events in Rust](https://stackoverflow.com/q/40516510/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Oct 28 '19 at 17:57
  • In my research I didn't find the thread "How to set a variable inside a gtk-rs closure?", but this also answers my question. – jonryan Oct 29 '19 at 09:02

1 Answers1

1

Once your string has been moved inside the closures, the compiler can no longer check statically that your are not mixing read and write accesses to it. You need to use a RefCell to enable runtime selection of read/write accesses, probably combined with Rc for proper memory management:

let firstname = Rc::new(RefCell::new(String::new()));
let firstname_clone = firstname.clone();
// ...
buttonSubmit.connect_clicked(move |_| {
    firstname.replace(entryFirstname.get_buffer().get_text());
});
// ...
buttonShow.connect_clicked(move |_| {
    println!("Firstname: {}", firstname_clone.borrow());
});
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jmb
  • 18,893
  • 2
  • 28
  • 55