Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- enum List {
- Cons(Rc<RefCell<i32>>, Rc<List>), // Rcの中にさらにRefCellを入れ込む
- Nil,
- }
- use List::{Cons, Nil};
- use std::rc::Rc; // 複数の所有者を持てる
- use std::cell::RefCell; // 不変扱いの値を変更できる
- fn main() {
- let value = Rc::new(RefCell::new(5));
- let a = Rc::new(Cons(Rc::clone(&value), Rc::new(Nil)));
- let b = Cons(Rc::new(RefCell::new(6)), Rc::clone(&a)); // 複数の所有者に可変値を配る
- let c = Cons(Rc::new(RefCell::new(10)), Rc::clone(&a));
- *value.borrow_mut() += 10; // 共有する値を変更
- println!("a after = {:?}", a); // それぞれの値が変更されていることを確認
- println!("b after = {:?}", b);
- println!("c after = {:?}", c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement