Using Stepby in for

Is there a way to use StepBy in he range, like:

for n in 1..=4 StepBy(2) {
      println!("{}", n)

output: 1, 3

Sure! 1..=4 produces an Iterator, so you can use all methods on it that the Iterator trait provides:

for n in (1..=4).step_by(2) {
    println!("{}", n)

Also note how the documentation of StepBy says:

This struct is created by the step_by method on Iterator.

StepBy is not itself a method that you could call.

1 Like