title: Is there a simple way to mutate an enum field in Rust?
body: <p>Suppose we have an enum that looks like this:</p>
  
  <pre><code>enum MyEnum {
      Field1,
      Field2 {x: f64, y: f64},
      /* Maybe some other fields */
      MyString(String),
  }
  </code></pre>
  
  <p>Now I created an instance of this enum of the subtype <code>MyString</code> and after some actions I want to mutate it. For example:</p>
  
  <pre><code>fn main() {
      let mut my_enum = MyEnum::MyString("Hello, world".to_string());
      /* Some actions */
      // Mutating the string
      match my_enum {
          MyEnum::MyString(ref mut content) =&gt; {
              content.push('!');
          },
          _ =&gt; {}
      }
      // Printing the string
      match my_enum {
          MyEnum::MyString(content) =&gt; {
              println!("{}", content);
          },
          _ =&gt; {}
      }
  }
  </code></pre>
  
  <p>However, matching in such a way is pretty cumbersome when we know exactly from the context that <code>my_enum</code> can be <strong>only</strong> <code>MyString</code>. I would rather write something like this (not a correct Rust syntax):</p>
  
  <pre><code>my_enum@MyString.push('!');
  println!("{}", my_enum@MyString);
  </code></pre>
  
  <p>And if, suppose, <code>my_enum</code> is of the subtype <code>Field2</code>, then to mutate <code>x</code>:</p>
  
  <pre><code>my_enum@Field2.x += 1.0;
  </code></pre>
  
  <p>Can I do something like this? I strongly suppose that the answer is "No", because if I remove <code>_ =&gt; {}</code> from the matches above, type checker starts complaining about non-exhaustive pattern matching:</p>
  
  <pre><code>patterns `Field1` and `Field2` not covered
  </code></pre>
  
  <p>though it can be inferred that <code>my_enum</code> can be nothing but <code>MyString</code>. By "inferred" I mean the compiler could track for all the variables of the type <code>MyEnum</code> what subtypes of values they can contain exactly.</p>
  
  <p>I found a place in a larger code where this could be convenient, though I guess I can rewrite it in other way. However, I think that the compiler could be smarter and at least understand that in this context the pattern <code>MyEnum::MyString</code> is exhaustive. If the answer on the question above is really "No", as I suspect, I'm interested if this issue was discussed among Rust developers (maybe a RFCS link?) and if it is worth to make a feature request.</p>