Created
June 27, 2019 14:36
-
-
Save octave99/b71e4f82def619aae22fca2eaec640ef to your computer and use it in GitHub Desktop.
This file contains 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
use std::cell::RefCell; | |
use std::rc::Rc; | |
pub struct MyStruct { | |
name: String, | |
} | |
fn error_fn() { | |
let mut ms = MyStruct { | |
name: "Before".to_owned(), | |
}; | |
let mut change1 = || ms.name = "After".to_owned(); | |
change1(); | |
// let mut change2 = move || ms.name = "After After".to_owned(); //note: move occurs because `ms` has type `MyStruct`, which does not implement the `Copy` trait | |
} | |
fn rs_fn() { | |
let rc = Rc::new(RefCell::new(MyStruct { | |
name: "Before".to_owned(), | |
})); | |
let change1 = || rc.borrow_mut().name = "After".to_owned(); | |
let change2 = || rc.borrow_mut().name = "After".to_owned(); | |
change1(); | |
change2(); | |
} | |
fn main() { | |
error_fn(); | |
rs_fn(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment