< 返回版块

jojoGyro 发表于 2023-07-13 10:38

Tags:rust编译

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

评论区

写评论
作者 jojoGyro 2023-07-14 17:26

大佬,我用play试了可以了,是vscode的问题,有时候会有编译问题

--
👇
QingyaoLin: 请把完整的代码用 https://play.rust-lang.org/ 分享出来。另外,请参考 https://doc.rust-lang.org/std/cell/index.html#introducing-mutability-inside-of-something-immutable

QingyaoLin 2023-07-13 13:12

请把完整的代码用 https://play.rust-lang.org/ 分享出来。另外,请参考 https://doc.rust-lang.org/std/cell/index.html#introducing-mutability-inside-of-something-immutable

1 共 2 条评论, 1 页