title: How can I improve the performance of element-wise multiplication in Rust?
body: <p>I will be doing element-wise multiplication on multiple vectors with 10^6+ elements. This is being flagged in profiling as one of the slowest parts of my code, so how can I improve it?</p>
  
  <pre><code>/// element-wise multiplication for vecs
  pub fn vec_mul&lt;T&gt;(v1: &amp;Vec&lt;T&gt;, v2: &amp;Vec&lt;T&gt;) -&gt; Vec&lt;T&gt;
  where
      T: std::ops::Mul&lt;Output = T&gt; + Copy,
  {
      if v1.len() != v2.len() {
          panic!("Cannot multiply vectors of different lengths!")
      }
      let mut out: Vec&lt;T&gt; = Vec::with_capacity(v1.len());
      for i in 0..(v1.len()) {
          out.push(v1[i] * v2[i]);
      }
      out
  }
  </code></pre>