- 263 名前:デフォルトの名無しさん mailto:sage [2021/11/22(月) 21:10:18.70 ID:ejqG4gpN.net]
- >>216
プログラマを守るためそれをさせないのがRustっていう言語ではw Rc<RefCell>ではいかんの? https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=50e9fc65e69dd1859478ee62dde963aa fn main() { use std::collections::LinkedList; use std::rc::Rc; use std::cell::RefCell; let mut list: LinkedList<Rc<RefCell<i32>>> = LinkedList::new(); list.push_back(Rc::new(RefCell::new(1))); list.push_back(Rc::new(RefCell::new(2))); list.push_back(Rc::new(RefCell::new(3))); println!("{:?}", list); // [RefCell { value: 1 }, RefCell { value: 2 }, RefCell { value: 3 }] let mut vec: Vec<Rc<RefCell<i32>>> = Vec::new(); let mut it = list.iter(); vec.push(Rc::clone(it.next().unwrap())); vec.push(Rc::clone(it.next().unwrap())); vec.push(Rc::clone(it.next().unwrap())); println!("{:?}", vec); // [RefCell { value: 1 }, RefCell { value: 2 }, RefCell { value: 3 }] println!("{:?}", vec[1]); // RefCell { value: 2 } *vec[1].borrow_mut() = 22; println!("{:?}", vec[1]); // RefCell { value: 22 } println!("{:?}", list); // [RefCell { value: 1 }, RefCell { value: 2 }, RefCell { value: 3 }] println!("{:?}", vec); // [RefCell { value: 1 }, RefCell { value: 2 }, RefCell { value: 3 }] }
|

|