title: How do I add two Rust arrays element-wise?
body: <p>This is an absolute beginner question, but I can't find anything useful after searching for half an hour. I have Rust 1.7.0 and this code:</p>
  
  <pre><code>type coord = [i64; 3];
  
  // add two coordinates ("vectors") pointwise, that is
  // if z = add(a, b) then z[i] = a[i] + b[i] for i=0..2
  fn add(a: coord, b: coord) -&gt; coord {
      //???
  }
  </code></pre>
  
  <p>The obvious thing I tried first is <code>a.zip(b).map(|(u,v)| u+v)</code> but this doesn't work (can't zip arrays), nor does <code>a.iter().zip(b.iter()).map(|(u,v)| u+v)</code> because it can't convert the iterator back to an array. I can see why this doesn't work in general but in this case we know both things are the same length.</p>
  
  <p>For now I'm doing</p>
  
  <pre><code>fn add(a: coord, b: coord) -&gt; coord {
      let mut z: coord = [0, 0, 0];
      for i in 0..2 {
          z[i] = a[i] + b[i];
      }
      z
  }
  </code></pre>
  
  <p>but it looks ugly by comparison. What am I missing?</p>