Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{rc::Rc, cell::RefCell};
- #[derive(Debug)]
- struct CoolThing {
- a: i32,
- b: String,
- c: f32,
- }
- trait CoolThingTrait {
- fn foo(self: Rc<Self>);
- fn bar(self: Rc<Self>);
- }
- impl CoolThingTrait for RefCell<CoolThing> {
- fn foo(self: Rc<Self>) {
- let mut ct = self.borrow_mut();
- ct.a += 8;
- ct.b = "asdf".to_string();
- ct.c = 4321.0;
- }
- fn bar(self: Rc<Self>) {
- let ct = self.borrow();
- println!("{:?}", *ct);
- }
- }
- fn main() {
- let ct = Rc::new(RefCell::new(CoolThing {
- a: 42,
- b: "hello world".to_string(),
- c: 1234.0,
- }));
- let ct_a = Rc::clone(&ct);
- let ct_b = Rc::clone(&ct);
- ct_a.foo();
- ct_b.bar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement