Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement