[英]Rust - cannot borrow as mutable
I would to understand how to insert value in a node in a binary tree:我想了解如何在二叉树的节点中插入值:
use std::rc::Rc;
use std::cell::RefCell;
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None
}
}
}
/*
1
/ \
2 3
/
4
*/
fn main() {
let mut t = TreeNode::new(1);
t.left = Some(Rc::new(RefCell::new (TreeNode::new(2))));
t.right = Some(Rc::new(RefCell::new(TreeNode::new(3))));
t.left.unwrap().get_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(4)))); // throws error
}
The tree should look like the one as in the comment.树应该看起来像评论中的那个。 On compiling it throws the error:编译时会抛出错误:
error[E0596]: cannot borrow data in an `Rc` as mutable
--> src/main.rs:35:5
|
35 | t.left.unwrap().get_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
| ^^^^^^^^^^^^^^^ cannot borrow as mutable
|
= help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Rc<RefCell<TreeNode>>`
How can I assign a new TreeNode to the left of value 2
?如何将新的 TreeNode 分配到值2
的左侧?
Update: I can't change the type of left
and right
from Option<Rc<RefCell<TreeNode>>>
to anything else.更新:我无法将left
类型从 Option<Rc< right
Option<Rc<RefCell<TreeNode>>>
更改为其他任何内容。 Thanks!谢谢!
Maybe you meant borrow_mut()
instead of get_mut()
也许你的意思是borrow_mut()
而不是get_mut()
fn main() {
let mut t = TreeNode::new(1);
t.left = Some(Rc::new(RefCell::new(TreeNode::new(2))));
t.right = Some(Rc::new(RefCell::new(TreeNode::new(3))));
t.left.as_ref().unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
}
With the updated question in mind, the incantation you're looking for is考虑到更新后的问题,您正在寻找的咒语是
t.left.unwrap().borrow_mut().left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
get_mut
is for when you already have a mutable reference to the RefCell
, and you never have a mutable reference to a thing in an Rc
for safety reasons. get_mut
用于当您已经拥有对RefCell
的可变引用,并且出于安全原因您永远不会对Rc
中的事物进行可变引用时。 borrow_mut
, on the other hand, is for circumventing the usual rules and getting a mutable reference to an immutable thing regardless.另一方面, borrow_mut
用于规避通常的规则并获得对不可变事物的可变引用。 Note that, in either case, the Rc
is transparent, since the Deref
trait takes care of it for us.请注意,在任何一种情况下, Rc
都是透明的,因为Deref
特性会为我们处理它。
It's wordy and clunky, and frankly I blame Leetcode for that.它既冗长又笨拙,坦率地说,我为此责怪 Leetcode。 I suspect they told someone to go translate everything into Rust, and that person did not know Rust and just threw everything in a RefCell
because they didn't know better.我怀疑他们告诉某人 go 将所有内容翻译成 Rust,而那个人不知道 Rust,只是将所有内容都扔到了RefCell
中,因为他们不知道更好。 So please don't take this as a good representation of idiomatic Rust.所以请不要把它当作惯用的 Rust 的良好代表。 That sample code is ugly, plain and simple.该示例代码丑陋,简单明了。 Rust is a much prettier language than that, I promise. Rust 是比这更漂亮的语言,我是 promise。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.