Created
September 2, 2016 08:57
-
-
Save zzeroo/bd26dc92a8825cb93ac86c3a0539aa63 to your computer and use it in GitHub Desktop.
Mutable after move
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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