title: Is it possible to take temporary ownership of a struct field?
body: <p>I have an immutable data structure, and an update function that takes ownership of the data structure and returns a new data structure:</p>
  
  <pre><code>enum Immutable {
      Item(i32)
  }
  
  fn update(imm: Immutable) -&gt; Immutable {
      match imm {
          Immutable::Item(x) =&gt; Immutable::Item(x + 1)
      }
  }
  </code></pre>
  
  <p>I need to store the data structure in a mutable field of a container:</p>
  
  <pre><code>struct State {
      item: Immutable
  }
  </code></pre>
  
  <p>I want to write an imperative update function for <code>State</code> that calls the function updater:</p>
  
  <pre><code>fn update_mut(st: &amp;mut State) -&gt; () {
      let mut owned = Immutable::Item(42); // junk
      std::mem::swap(&amp;mut st.item, &amp;mut owned);
      st.item = update(owned);
  }
  </code></pre>
  
  <p>This code works, but it seems sily to use <code>mem::swap</code> and allocate a junk object. I would really like to write:</p>
  
  <pre><code>fn update_mut_type_error(st: &amp;mut State) -&gt; () {
      let mut owned = Immutable::Item(42); // junk
      std::mem::swap(&amp;mut st.item, &amp;mut owned);
      st.item = update(st.item); // type error
  }
  </code></pre>
  
  <p>Is there any way to address this? Or, do I have to use <code>mem::swap</code> here, even though it seems spurious.</p>
  
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=e4d2e7687cb5fb2eb2c1489138142ef6" rel="nofollow noreferrer">Example on Rust Playground</a></p>