Rc不支持内部更改RefCell,官方文档为什么这么写,我的rustc版本是1.7 `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(3)), Rc::clone(&a)); let c = Cons(Rc::new(RefCell::new(4)), Rc::clone(&a)); *value.borrow_mut() += 10; println!("a after = {:?}", a); println!("b after = {:?}", b); println!("c after = {:?}", c);
} `
error[E0368]: binary assignment operation +=
cannot be applied to type Rc<RefCell<i32>>
--> .\main.rs:11:5
|
11 | *value.borrow_mut() += 10;
| |
| cannot use +=
on type Rc<RefCell<i32>>
error: aborting due to previous error
评论区
写评论大佬,我用play试了可以了,是vscode的问题,有时候会有编译问题
--
👇
QingyaoLin: 请把完整的代码用 https://play.rust-lang.org/ 分享出来。另外,请参考 https://doc.rust-lang.org/std/cell/index.html#introducing-mutability-inside-of-something-immutable
请把完整的代码用 https://play.rust-lang.org/ 分享出来。另外,请参考 https://doc.rust-lang.org/std/cell/index.html#introducing-mutability-inside-of-something-immutable