title: How can I sum a range of numbers in Rust?
body: <p>I want to write a function to sum the numbers from zero to n. (Ideally, this would be generic over all numbers, but I will settle for <code>i32</code>).</p>
  
  <pre><code>mod squares {
  
      pub fn sum_from_zero( n: i32) -&gt; i32 {
          [0 .. n].fold(0, |a, b| a + b)
      }
  }
  
  #[test]
  fn test_sum_from_zero() {
      assert_eq!(15, squares::sum_from_zero(5));
  }
  </code></pre>
  
  <p>I get the following compiler error:</p>
  
  <pre><code>src/lib.rs:5:18: 5:22 error: no method named `fold` found for type `[std::ops::Range&lt;i32&gt;; 1]` in the current scope
  src/lib.rs:5         [0 .. n].fold(0, |a, b| a + b)
                                ^~~~
  src/lib.rs:5:18: 5:22 note: the method `fold` exists but the following trait bounds were not satisfied: `[std::ops::Range&lt;i32&gt;; 1] : std::iter::Iterator`, `[std::ops::Range&lt;i32&gt;] : std::iter::Iterator`
  </code></pre>
  
  <p>I've also tried this with <code>sum()</code>:</p>
  
  <pre><code>mod squares {
  
      pub fn sum_from_zero( n: i32) -&gt; i32 {
          [0 .. n].sum()
      }
  }
  
  #[test]
  fn test_sum_from_zero() {
      assert_eq!(15, squares::sum_from_zero(5));
  }
  </code></pre>
  
  <p>And got the following compiler error:</p>
  
  <pre><code>src/lib.rs:5:18: 5:21 error: no method named `sum` found for type `[std::ops::Range&lt;i32&gt;; 1]` in the current scope
  src/lib.rs:5         [0 .. n].sum()
                                ^~~
  src/lib.rs:5:18: 5:21 note: the method `sum` exists but the following trait bounds were not satisfied: `[std::ops::Range&lt;i32&gt;; 1] : std::iter::Iterator`, `[std::ops::Range&lt;i32&gt;] : std::iter::Iterator`
  src/lib.rs:5:18: 5:21 error: no method named `sum` found for type `[std::ops::Range&lt;i32&gt;; 1]` in the current scope
  src/lib.rs:5         [0 .. n].sum()
                                ^~~
  src/lib.rs:5:18: 5:21 note: the method `sum` exists but the following trait bounds were not satisfied: `[std::ops::Range&lt;i32&gt;; 1] : std::iter::Iterator`, `[std::ops::Range&lt;i32&gt;] : std::iter::Iterator`
  </code></pre>
  
  <p>Do I have to declare explicit bounds/traits? </p>