title: Modify struct field inside of Option
body: <p>I'm having problems in changing a field of a struct inside of option. This is the code:</p>
  
  <pre><code>struct MyStruct {
      field1 : i32,
      field2 : i32,
      // and many more...
  }
  
  impl MyStruct {
      pub fn field1(&amp;mut self, field1 : i32) -&gt; &amp;mut Self {
          self.field1 = field1;
          self
      }
  }
  
  fn foo() -&gt; Option&lt;MyStruct&gt; {
      None
  }
  
  fn bar() -&gt; Option&lt;MyStruct&gt; {
      foo().as_mut().map(|s| s.field1(5))
  }
  
  fn main() {
      bar();
  }
  </code></pre>
  
  <p>The main idea of <code>bar()</code> is to get an <code>Option&lt;MyStruct&gt;</code> from another function returning an <code>Option&lt;MyStruct&gt;</code>, change a field of that struct (in case the result is not <code>None</code>) and return the resulting <code>Option&lt;MyStruct&gt;</code>.</p>
  
  <p>The struct implements the builder pattern, so I've used it.</p>
  
  <p>In this case I get the following error:</p>
  
  <pre><code>test.rs:18:5: 18:40 error: mismatched types:
  expected `core::option::Option&lt;MyStruct&gt;`,
      found `core::option::Option&lt;&amp;mut MyStruct&gt;`
  (expected struct `MyStruct`,
      found &amp;-ptr) [E0308]
  test.rs:18     foo().as_mut().map(|s| s.field1(5))
  </code></pre>
  
  <p>I've also tried to use <code>Option.take()</code> and to change the signature of the builder method as <code>pub fn field1(mut self, field1 : i32) -&gt; Self</code>, but none of them work.</p>
  
  <p>How can I make this code work?</p>
  
  <p>I know that I could create another <code>Option</code> and map every field of the input to the corresponding field of the output, but <code>MyStruct</code> has a lot of fields and it is tedious.</p>