title: A struct with a trait field, but optional
body: <p>Say I have a struct whose implementation writes <em>somewhere</em>, i.e. to something that implements the <code>std::io::Write</code> trait. However, I don't want the struct to own this. The following code works:</p>
  <pre><code>fn main() {
      let mut out = std::io::stdout();
      let mut foo = Foo::new(&amp;mut out);
      foo.print_number(2);
  }
  
  struct Foo&lt;'a&gt; {
      out: &amp;'a mut dyn std::io::Write
  }
  
  impl&lt;'a&gt; Foo&lt;'a&gt; {
      pub fn new(out: &amp;'a mut dyn std::io::Write) -&gt; Self {
          Self {
              out
          }
      }
      
      pub fn print_number(&amp;mut self, i: isize) {
          writeln!(self.out, &quot;The number is {}&quot;, i).unwrap()
      }
  }
  </code></pre>
  <p>But, now this writing functionality should be made optional. I thought this sounds easy enough, but now the following doesn't compile:</p>
  <pre><code>fn main() {
      let mut out = std::io::stdout();
      let mut foo = Foo::new(Some(&amp;mut out));
      foo.print_number(2);
  }
  
  struct Foo&lt;'a&gt; {
      out: Option&lt;&amp;'a mut dyn std::io::Write&gt;
  }
  
  impl&lt;'a&gt; Foo&lt;'a&gt; {
      pub fn new(out: Option&lt;&amp;'a mut dyn std::io::Write&gt;) -&gt; Self {
          Self {
              out
          }
      }
      
      pub fn print_number(&amp;mut self, i: isize) {
          if self.out.is_some() {
              writeln!(self.out.unwrap(), &quot;The number is {}&quot;, i).unwrap()
          }
      }
  }
  </code></pre>
  <p>because of:</p>
  <pre><code>error[E0507]: cannot move out of `self.out` which is behind a mutable reference
          --&gt; src/main.rs:20:26
          |
          20 |                 writeln!(self.out.unwrap(), &quot;The number is {}&quot;, i).unwrap()
          |                          ^^^^^^^^
          |                          |
          |                          move occurs because `self.out` has type `Option&lt;&amp;mut dyn std::io::Write&gt;`, which does not implement the `Copy` trait
          |                          help: consider borrowing the `Option`'s content: `self.out.as_ref()`
  </code></pre>
  <p>which I'm not sure how to interpret.</p>
  <p>I tried following the suggestion by changing the line in question to:</p>
  <pre><code>writeln!(self.out.as_ref().unwrap(), &quot;The number is {}&quot;, i).unwrap()
  </code></pre>
  <p>but then I get</p>
  <pre><code>error[E0596]: cannot borrow data in a `&amp;` reference as mutable
  --&gt; src/main.rs:20:26
  |
  20 |                 writeln!(self.out.as_ref().unwrap(), &quot;The number is {}&quot;, i).unwrap()
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
  </code></pre>
  <p>I'm really not sure how to interpret these error messages and surprisingly I'm not really getting anywhere by just sprinkling <code>&amp;</code>s and <code>mut</code>s in random places without really understanding!</p>
  <p>(As an aside, I'm not sure if this is a &quot;good&quot; way of going about this anyway? I'm open to completely different approaches of solving this problem, which is basically to optionally pass something to write to into a struct, but without the struct owning it. I read about the <code>Box</code> type which might also be relevant?)</p>