Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{cell::RefCell, rc::Rc};
- #[derive(Default)]
- struct Loopy {
- thing: RefCell<Option<Box<Fn()>>>,
- }
- impl Loopy {
- fn tie_the_knot(self: Rc<Self>) {
- let me = self.clone();
- *self.thing.borrow_mut() = Some(Box::new(move || {
- println!("Is set? {}", me.thing.borrow().is_some());
- }))
- }
- }
- fn main() {
- let l = Rc::new(Loopy::default());
- l.clone().tie_the_knot();
- let f = l.thing.borrow();
- if let Some(f) = &*f {
- f();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement