9

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 i32).

mod squares {

    pub fn sum_from_zero( n: i32) -> i32 {
        [0 .. n].fold(0, |a, b| a + b)
    }
}

#[test]
fn test_sum_from_zero() {
    assert_eq!(15, squares::sum_from_zero(5));
}

I get the following compiler error:

src/lib.rs:5:18: 5:22 error: no method named `fold` found for type `[std::ops::Range<i32>; 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<i32>; 1] : std::iter::Iterator`, `[std::ops::Range<i32>] : std::iter::Iterator`

I've also tried this with sum():

mod squares {

    pub fn sum_from_zero( n: i32) -> i32 {
        [0 .. n].sum()
    }
}

#[test]
fn test_sum_from_zero() {
    assert_eq!(15, squares::sum_from_zero(5));
}

And got the following compiler error:

src/lib.rs:5:18: 5:21 error: no method named `sum` found for type `[std::ops::Range<i32>; 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<i32>; 1] : std::iter::Iterator`, `[std::ops::Range<i32>] : std::iter::Iterator`
src/lib.rs:5:18: 5:21 error: no method named `sum` found for type `[std::ops::Range<i32>; 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<i32>; 1] : std::iter::Iterator`, `[std::ops::Range<i32>] : std::iter::Iterator`

Do I have to declare explicit bounds/traits?

1
  • 1
    You're creating a single-element array of Range<i32> types (aka [std::ops::Range<i32>; 1]). You probably just mean (0 .. n), which just changes operator precedence so that you're not invoking sum() on n. (Playground Example)
    – Aurora0001
    Oct 22, 2016 at 17:29

2 Answers 2

17

The problem is that you are creating an array of ranges (square brackets) but you just wanted the range (on which fold is defined).

Another thing is that range syntax (..) is only inclusive of the lower bound. It's exclusive of the upper bound so you have to iterate up to n+1 to get the desired result.

mod squares {

    pub fn sum_from_zero( n: i32) -> i32 {
        (0 .. n+1).fold(0, |a, b| a + b)
    }
}

#[test]
fn test_sum_from_zero() {
    assert_eq!(15, squares::sum_from_zero(5));
}
7
  • 4
    As of Rust 1.11, the Iterator trait has a sum method and ranges are iterators, so no need to fold. Also, no mention of n×(n+1)/2?
    – mcarton
    Oct 22, 2016 at 18:22
  • 2
    @mcarton: Let's presume that he is interested in learning how to iterate and not in the closed form :) Oct 22, 2016 at 19:37
  • 1
    FWIW, on current stable Rust (release 1.9.0) I cannot use sum (github.com/rust-lang/rust/issues/27739).
    – Jon Wolski
    Oct 22, 2016 at 20:07
  • 4
    @JonWolski Current stable is 1.12.1
    – Arjan
    Oct 22, 2016 at 20:20
  • 1
    now rust can use 0..=n and 0..(n+1) two ways. Jan 6, 2022 at 11:53
1

You can use the Iterator trait, which includes the sum method, to sum a range of numbers. Here's an example implementation:

mod squares {

    pub fn sum_from_zero(n: i32) -> i32 {
        (0..=n).into_iter().sum()
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.