Rust 与借书人搏斗

Rust 与借书人搏斗,rust,Rust,我是新手。作为一个学习练习,我试图做一个基本的二叉树。这就是我到目前为止所做的: 我已经把自己卷入了所有的裁判、穆特和&'s和*'s中,我不知道如何走出去。我哪里出错了?您有两个问题: 无法移出借用的上下文:有关说明,请参阅 无法分配给不可变字段:您只有一个&节点;要修改节点,您需要一个&mut节点mut curr仅使绑定可变,这意味着您可以为curr指定一个新值。但是,您不能修改curr引用的内容。在整个代码中传播&-to-&mut转换,它就会工作 既然你是个生锈新手,看看我是怎么写的可能

我是新手。作为一个学习练习,我试图做一个基本的二叉树。这就是我到目前为止所做的:

我已经把自己卷入了所有的裁判、穆特和&'s和*'s中,我不知道如何走出去。我哪里出错了?

您有两个问题:

  • 无法移出借用的上下文:有关说明,请参阅

  • 无法分配给不可变字段:您只有一个
    &节点
    ;要修改
    节点
    ,您需要一个
    &mut节点
    <模式中的code>mut curr仅使绑定可变,这意味着您可以为
    curr
    指定一个新值。但是,您不能修改
    curr
    引用的内容。在整个代码中传播
    &
    -to-
    &mut
    转换,它就会工作


既然你是个生锈新手,看看我是怎么写的可能会有帮助:

struct Node<T> {
    value: T,
    left: Option<Box<Node<T>>>,
    right: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(x: T) -> Node<T> {
        Node { value: x, left: None, right: None }
    }
    fn boxed(x: T) -> Box<Node<T>> {
        Box::new(Node::new(x))
    }
}

fn insert<T: PartialOrd>(root: &mut Option<Box<Node<T>>>, new: Box<Node<T>>) {
    if let Some(ref mut rbx) = *root {
        if new.value < rbx.value {
            insert(&mut rbx.left, new);
        } else {
            insert(&mut rbx.right, new);
        }
    } else {
        *root = Some(new);
    }
}

fn main() {
    let data = vec![6,1,2,3,4,5];
    let mut root = None;
    for val in data {
        insert(&mut root, Node::boxed(val));
    }
    println!("Root value: {}", root.unwrap().value); 
}
struct节点{
值:T,
左:选项,
右:选项,
}
impl节点{
fn新(x:T)->节点{
节点{值:x,左:无,右:无}
}
fn盒装(x:T)->盒装{
框::新建(节点::新建(x))
}
}
fn插入(根:&mut选项,新:框){
如果让一些(ref mut rbx)=*根{
如果new.value
我意识到这更像是一个练习,但请记住,这种数据结构不应增长到超过某个树深度,因为它可能会在递归释放节点时导致堆栈溢出。

可能的重复
test.rs:21:48: 21:52 error: cannot move out of borrowed content
test.rs:21             None => curr.right = Some(Box::new(*new))
                                                          ^~~~
test.rs:26:47: 26:51 error: cannot move out of borrowed content
test.rs:26             None => curr.left = Some(Box::new(*new))
                                                         ^~~~
test.rs:21:21: 21:54 error: cannot assign to immutable field `curr.right`
test.rs:21             None => curr.right = Some(Box::new(*new))
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.rs:26:21: 26:53 error: cannot assign to immutable field `curr.left`
test.rs:26             None => curr.left = Some(Box::new(*new))
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 4 previous errors
struct Node<T> {
    value: T,
    left: Option<Box<Node<T>>>,
    right: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(x: T) -> Node<T> {
        Node { value: x, left: None, right: None }
    }
    fn boxed(x: T) -> Box<Node<T>> {
        Box::new(Node::new(x))
    }
}

fn insert<T: PartialOrd>(root: &mut Option<Box<Node<T>>>, new: Box<Node<T>>) {
    if let Some(ref mut rbx) = *root {
        if new.value < rbx.value {
            insert(&mut rbx.left, new);
        } else {
            insert(&mut rbx.right, new);
        }
    } else {
        *root = Some(new);
    }
}

fn main() {
    let data = vec![6,1,2,3,4,5];
    let mut root = None;
    for val in data {
        insert(&mut root, Node::boxed(val));
    }
    println!("Root value: {}", root.unwrap().value); 
}