Skip to content

Instantly share code, notes, and snippets.

@octave99
Created June 27, 2019 14:36
Show Gist options
  • Save octave99/b71e4f82def619aae22fca2eaec640ef to your computer and use it in GitHub Desktop.
Save octave99/b71e4f82def619aae22fca2eaec640ef to your computer and use it in GitHub Desktop.
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