Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sum a range of numbers in Rust?

Tags:

rust

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?

like image 275
Jon Wolski Avatar asked Oct 22 '16 17:10

Jon Wolski


People also ask

How do you sum an array in Rust?

What must I do to sum an array of bytes? In |&x| (x - 48) , those parentheses are unnecessary; |&x| x - 48 will work fine. You can write sum += byte - 48; for the manual loop. Also, you use a manual fold: numbers.

How do you find the sum of the range?

Here's a formula that uses two cell ranges: =SUM(A2:A4,C2:C3) sums the numbers in ranges A2:A4 and C2:C3. You'd press Enter to get the total of 39787. To create the formula: Type =SUM in a cell, followed by an opening parenthesis (.

What is range rust?

The range operator allows you to iterate over a collection of items as specified by the start and ending offset. This article will discuss common range expressions and how to use them in Rust.


1 Answers

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));
}
like image 160
Arjan Avatar answered Oct 15 '22 00:10

Arjan