title: How do I access T in an Option<T> without causing a move?
body: <p>I'm working on an insert function in a binary search tree implementation. Here's my code:</p>
  
  <pre><code>pub struct Node {
      data: i32,
      left: Option&lt;Box&lt;Node&gt;&gt;,
      right: Option&lt;Box&lt;Node&gt;&gt;
  }
  
  fn insert_at_root(mut root_node: Node, new_node: Node) -&gt; Node { //not reference because root_node will be mutated
      if root_node.data &gt; new_node.data { // value less than root
          if let Some(left) = root_node.left {
              insert_node(*left, new_node); // *left is a way to downcast box, i.e. *left = T from Box&lt;T&gt;
          }
          else {
              root_node.set_left(Some(Box::new(new_node)));
          }
      }
      else if root_node.data &lt; new_node.data {
          if let Some(right) = root_node.right {
              insert_node(*right, new_node);
          }
          else {
              root_node.set_right(Some(Box::new(new_node)));
          }
      }
      root_node
  }
  
  fn insert_node(mut exist_node: Node, new_node: Node) -&gt; () {
      if exist_node.data &gt; new_node.data {
          if let Some(left) = exist_node.left {
              insert_node(*left, new_node);
          }
          else {
              exist_node.set_left(Some(Box::new(new_node)));
          }
      }
      else if exist_node.data &lt; new_node.data {
          if let Some(right) = exist_node.right {
              insert_node(*right, new_node);
          }
          else {
              exist_node.set_right(Some(Box::new(new_node)));
          }
      }
  }
  </code></pre>
  
  <p>I have two insert functions so I can preserve the variable with the root node when I call insert_at_node.</p>
  
  <p>My current problem is the line <code>if let Some(left) = root_node.left {</code> (and the line <code>if let Some(right) = root_node.right {</code>) in the <code>insert_at_root</code> function which apparently causes a move. As a result, I can't return <code>root_node</code> at the end of <code>insert_at_node</code>:</p>
  
  <pre><code>error[E0382]: use of moved value: `root_node`
    --&gt; src/lib.rs:34:5
     |
  19 |         if let Some(left) = root_node.left {
     |                     ---- value moved here
  ...
  34 |     root_node
     |     ^^^^^^^^^ value used here after partial move
     |
     = note: move occurs because value has type `std::boxed::Box&lt;Node&gt;`, which does not implement the `Copy` trait
  
  error: aborting due to previous error
  </code></pre>
  
  <p>The purpose of those lines is to check if the <code>left</code> (or <code>right</code>) child node is not <code>None</code>, basically <code>root_node.left != None</code>.
  Is there any way to achieve this without causing a move? Maybe something with an <code>!=</code> or <code>==</code> sign.</p>