Data structures `无法移出对`&;的取消引用;在构建排序链表时使用mut`-pointer`

Data structures `无法移出对`&;的取消引用;在构建排序链表时使用mut`-pointer`,data-structures,rust,Data Structures,Rust,所以,我正在学习Rust并决定建立一个排序链表。在我找到add方法之前,一切看起来都很好,下面是代码: struct NodeItem<'a, V:'a + Ord> { value : V, next : Box<Option<NodeItem<'a,V>>> // ' } impl <'a, V:'a + Ord> NodeItem<'a,V> { // ' fn new(value : V) ->

所以,我正在学习Rust并决定建立一个排序链表。在我找到add方法之前,一切看起来都很好,下面是代码:

struct NodeItem<'a, V:'a + Ord> {
  value : V,
  next : Box<Option<NodeItem<'a,V>>> // '
}

impl <'a, V:'a + Ord> NodeItem<'a,V> { // '

  fn new(value : V) -> NodeItem<'a,V> { // '
    NodeItem { value : value, next : box None }
  }

  fn add(&mut self, value : V) {

    match self.value.cmp(&value) {
      Less => {
        self.next = box Some(NodeItem {value: self.value, next : self.next });
        self.value = value;
      },
      Equal | Greater => {
        match *self.next {
          Some(ref mut next) => next.add(value),
          None => self.next = box Some(NodeItem::new(value)),
        }
      },
    }

  }

}
这里到底有什么问题?我知道我正在将引用移动到其他地方,但生命周期参数不应该显示这些项具有相关的“生命”吗

这是从2014年12月21日开始的夜间活动。

这里有一个:

以及错误:

<anon>:7:13: 7:14 error: cannot move out of dereference of `&mut`-pointer
<anon>:7     let c = b.0;
                     ^
<anon>:7:9: 7:10 note: attempting to move value to here
<anon>:7     let c = b.0;
                 ^
<anon>:7:9: 7:10 help: to prevent the move, use `ref c` or `ref mut c` to capture value by reference
<anon>:7     let c = b.0;
                 ^

创建一个链接列表可以帮助您理解问题,也可以帮助我们帮助您。关于这种性质的链接列表,有很多问题。仔细阅读其中的一些内容也会很有用。克里斯·摩根(Chris Morgan)在哪里做了一个出色的解释。那么,当我需要“中间添加”语义时,如何表示数据结构?@MaurícioLinhares正如我在上一段中提到的,有多种方法可以解决您的问题。您必须决定什么适合您的解决方案。我可能有点困惑,但是Box::new(self)是否会制作self的新副本?
enum E { Hello }
struct A(E);

fn main() {
    let mut a = A(E::Hello);
    let b = &mut a;
    let c = b.0;
}
<anon>:7:13: 7:14 error: cannot move out of dereference of `&mut`-pointer
<anon>:7     let c = b.0;
                     ^
<anon>:7:9: 7:10 note: attempting to move value to here
<anon>:7     let c = b.0;
                 ^
<anon>:7:9: 7:10 help: to prevent the move, use `ref c` or `ref mut c` to capture value by reference
<anon>:7     let c = b.0;
                 ^
#[derive(Debug)]
struct Node<T> {
    v: T,
    next: Option<Box<Node<T>>>,
}

impl<T> Node<T> {
    fn new(v: T) -> Node<T> { Node { v: v, next: None } }

    fn push_front(self, head: T) -> Node<T> {
        Node {
            v: head,
            next: Some(Box::new(self)),
        }
    }

    fn push_back(&mut self, tail: T) {
        match self.next {
            Some(ref mut next) => next.push_back(tail), 
            None => self.next = Some(Box::new(Node::new(tail))),
        }
    }

    fn push_after(&mut self, v: T) {
        let old_next = self.next.take();

        let new_next = Node {
            v: v,
            next: old_next,
        };

        self.next = Some(Box::new(new_next));
    }
}

fn main() {
    let mut n = Node::new(2u8);
    n.push_back(3u8);
    let mut n = n.push_front(0u8);
    n.push_after(1u8);

    println!("{:?}", n);
}