Skip to content

Instantly share code, notes, and snippets.

@zzeroo
Created September 2, 2016 08:57
Show Gist options
  • Save zzeroo/bd26dc92a8825cb93ac86c3a0539aa63 to your computer and use it in GitHub Desktop.
Save zzeroo/bd26dc92a8825cb93ac86c3a0539aa63 to your computer and use it in GitHub Desktop.
Mutable after move
extern crate gtk;
use gtk::prelude::*;
#[derive(Clone)]
struct Client {
error_counter: i32,
}
impl Client {
fn new() -> Self {
Client { error_counter: 0, }
}
}
fn main() {
if gtk::init().is_err() {
println!("Failed to initialize GTK.");
return;
}
// Create a Client
let mut client = Client::new();
// Client is mutable
client.error_counter = 1;
let window = gtk::Window::new(gtk::WindowType::Toplevel);
let button = gtk::Button::new_with_label("Click me!");
window.set_title("Mutable after move?");
window.set_default_size(350, 70);
window.add(&button);
window.show_all();
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
{ // Client stays mutable, in block
client.error_counter = 2;
let mut client = client.clone();
button.connect_clicked(move |_| {
// Client is no more mutable
client.error_counter = 0;
});
}
// Client mus live til here >.<
client.error_counter = 0;
gtk::main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment