SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::{cell::RefCell, rc::Rc};
  2.  
  3. #[derive(Default)]
  4. struct Loopy {
  5.     thing: RefCell<Option<Box<Fn()>>>,
  6. }
  7.  
  8. impl Loopy {
  9.     fn tie_the_knot(self: Rc<Self>) {
  10.         let me = self.clone();
  11.         *self.thing.borrow_mut() = Some(Box::new(move || {
  12.             println!("Is set? {}", me.thing.borrow().is_some());
  13.         }))
  14.     }
  15. }
  16.  
  17. fn main() {
  18.     let l = Rc::new(Loopy::default());
  19.     l.clone().tie_the_knot();
  20.  
  21.     let f = l.thing.borrow();
  22.     if let Some(f) = &*f {
  23.         f();
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top