Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. use std::{rc::Rc, cell::RefCell};
  2.  
  3. #[derive(Debug)]
  4. struct CoolThing {
  5. a: i32,
  6. b: String,
  7. c: f32,
  8. }
  9.  
  10. trait CoolThingTrait {
  11. fn foo(self: Rc<Self>);
  12. fn bar(self: Rc<Self>);
  13. }
  14.  
  15. impl CoolThingTrait for RefCell<CoolThing> {
  16. fn foo(self: Rc<Self>) {
  17. let mut ct = self.borrow_mut();
  18. ct.a += 8;
  19. ct.b = "asdf".to_string();
  20. ct.c = 4321.0;
  21. }
  22.  
  23. fn bar(self: Rc<Self>) {
  24. let ct = self.borrow();
  25. println!("{:?}", *ct);
  26. }
  27. }
  28.  
  29. fn main() {
  30. let ct = Rc::new(RefCell::new(CoolThing {
  31. a: 42,
  32. b: "hello world".to_string(),
  33. c: 1234.0,
  34. }));
  35.  
  36. let ct_a = Rc::clone(&ct);
  37. let ct_b = Rc::clone(&ct);
  38.  
  39. ct_a.foo();
  40. ct_b.bar();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement