-1
    for mut i in 0..448 {
    i += 179;
    println!("{}", i)

How can I add all of the values that get printed to the terminal when this is ran?

codeg
  • 45
  • 4
  • 1
    What is this even supposed to be? Why would you write a loop like that rather than directly giving it a range of `179..627` (or `179..=626`)? Also, you need to include complete code blocks in your example code; where are the closing brackets? – Herohtar Aug 12 '21 at 19:30
  • This is essentially a duplicate of one of these: https://stackoverflow.com/questions/40194949/how-can-i-sum-a-range-of-numbers-in-rust or https://stackoverflow.com/questions/23100534/how-to-sum-the-values-in-an-array-slice-or-vec-in-rust – Herohtar Aug 12 '21 at 19:35

1 Answers1

1
    fn main() {
    let a = 179..627;
    let sum: i32 = a.into_iter().sum();
    println!("the total sum is: {}", sum);

this worked

codeg
  • 45
  • 4
  • 2
    Ranges are already iterators, so there is no need for `into_iter()`. The extra variable assignment is unnecessary as well. You can compute the sum with literally just `(179..627).sum()`. – Herohtar Aug 12 '21 at 19:41