2016-07-20 18 views
1

我想用兩個指針構建一個簡單的Node結構體,編譯器會抱怨我已經移動了。我瞭解錯誤,但我不知道如何解決這個問題。使用部分移動的值:`* head`

#[derive(Debug)] 
struct Node { 
    val: char, 
    left: Option<Box<Node>>, 
    right: Option<Box<Node>>, 
} 

impl Node { 
    fn new(c: char) -> Box<Node> { 
     let new_node = Node { 
      val: c, 
      left: None, 
      right: None, 
     }; 
     println!("new Node {}", c); 
     return Box::new(new_node); 
    } 
    pub fn add_left(&mut self, c: char) { 
     let n_node = Node::new(c); 
     let target = &mut self.left; 
     *target = Some(n_node); 
    } 
    pub fn add_right(&mut self, c: char) { 
     let n_node = Node::new(c); 
     let target = &mut self.right; 
     *target = Some(n_node); 
    } 
} 

fn main() { 
    println!("Hello, world!"); 
    let mut head = Node::new('M'); 
    head.add_left('C'); 
    head.left.unwrap().add_left('A'); 
    head.add_right('N'); 
} 

拋出以下

error: use of partially moved value: `*head` [E0382] 
    head.add_right('N'); 
    ^~~~ 
help: run `rustc --explain E0382` to see a detailed explanation 
note: `head.left` moved here because it has type `std::option::Option<Box<Node>>`, which is non-copyable 
    head.left.unwrap() .add_left('A'); 
+0

其實,我相信,如果你閱讀和理解http://stackoverflow.com/a/34279224/155423,並期待在您的整個問題將得到解決'unwrap'簽名。 – Shepmaster

回答