title: Multiply all elements in a generic vector?
body: <p>I'm trying to multiply all elements in a vector but I can't figure out how to do this with a reference. Here is an example:</p>
  
  <pre><code>struct S&lt;T&gt; {
      elements: Vec&lt;T&gt;,
  }
  
  impl&lt;T&gt; std::ops::MulAssign&lt;T&gt; for S&lt;T&gt;
  where
      T: std::ops::MulAssign,
  {
      fn mul_assign(&amp;mut self, val: T) {
          for i in 0..self.elements.len() {
              self.elements[i] *= val;
          }
      }
  }
  </code></pre>
  
  <p>This doesn't compile:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0382]: use of moved value: `val`
    --&gt; src/lib.rs:11:33
     |
  9  |     fn mul_assign(&amp;mut self, val: T) {
     |                              --- move occurs because `val` has type `T`, which does not implement the `Copy` trait
  10 |         for i in 0..self.elements.len() {
  11 |             self.elements[i] *= val;
     |                                 ^^^ value moved here, in previous iteration of loop
  </code></pre>
  
  <p>I've tried this with references in different ways, but I didn't succeed. How do you do this? I would prefer to implement this without the <code>Copy</code> trait.</p>