5

This question may seem extremely basic, but I'm having a hard time figuring out how to do this. I have an integer, and I need to use a for loop to loop integer number of times.

First, I tried -

fn main() {
    let number = 10; // Any value is ok
    for num in number {
        println!("success");
    }
}

this prints the error

error[E0277]: `{integer}` is not an iterator
 --> src/main.rs:3:16
  |
3 |     for num in number{
  |                ^^^^^^ `{integer}` is not an iterator
  |
  = help: the trait `std::iter::Iterator` is not implemented for `{integer}`
  = note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
  = note: required by `std::iter::IntoIterator::into_iter`

Next, I tried -

fn main() {
    let number = 10; // Any value is ok
    for num in number.iter() {
        println!("success");
    }
}

the compiler says there is no method iter for integer

error[E0599]: no method named `iter` found for type `{integer}` in the current scope
 --> src/main.rs:3:23
  |
3 |     for num in number.iter() {
  |                       ^^^^

How am I supposed to do this?

Lumin
  • 373
  • 6
  • 21
  • 3
    Possible duplicate of [How do you make a range in Rust?](https://stackoverflow.com/questions/9271970/how-do-you-make-a-range-in-rust) – hellow May 03 '19 at 07:34
  • Also worth reading: https://stackoverflow.com/q/27893223/ – hellow May 03 '19 at 07:34
  • 2
    Also relevant: [What is the idiomatic way to write a for loop without using the iterator value?](https://stackoverflow.com/questions/29932503/what-is-the-idiomatic-way-to-write-a-for-loop-without-using-the-iterator-value). – Lukas Kalbertodt May 03 '19 at 08:15

3 Answers3

10

This is because you are saying to the compiler for a num contained in number where number is not an iterator and neither does implement iter, rather than for a num in the range 0..number which is an iterator.

The documentation describes the for loop as:

for loop_variable in iterator {
    code()
}

Change the code to:

fn main() {
    let number = 10; 
    for num in 0..number { // change it to get range
        println!("success");
    }
}

You can also change it to:

fn main() {
    let number = 10; 
    for num in 1..=number { // inclusive range
        println!("success");
    }
}

Or to:

fn main() {
    let number = 10; 
    for _ in 0..number { // where _ is a "throw away" variable
        println!("success");
    }
}

Also see for documentation

sn99
  • 843
  • 8
  • 24
2

I would like to share this closure-based groovy-inspired way of looping n times in Rust.

pub trait Times {
    fn times(&self, f:fn(Self));
}

impl Times for u8 {
    fn times(&self, f:fn(u8)) {
        for x in 0..*self {
            f(x)
        }
    }
}

fn main() {
    const K: u8 =7;
    4.times( |v:u8| { println!("Inline Closure {v}.{K}"); } );
}

The output is:

Inline Closure 0.7
Inline Closure 1.7
Inline Closure 2.7
Inline Closure 3.7
1

Rust for-loops take an iterator (actually anything that can be converted into an iterator). An sole integer can not be converted into an iterator, but a range can.

fn main() {
    let number = 10; // Any value is ok
    for num in 0..number {
        println!("success");
    }
}

https://play.integer32.com/?version=stable&mode=debug&edition=2018&gist=029803cf8ac6efaa3113b2f32ae6ef0d

chpio
  • 896
  • 6
  • 16
  • How does your answer differ from the suggest duplicate? Please avoid adding redundant information. That's whats the duplicate flag is for. For more information see [help/duplicates](https://stackoverflow.com/help/duplicates). – hellow May 03 '19 at 07:43
  • sorry, that i haven't seen the comment while i was typing this. – chpio May 03 '19 at 07:44