Avatar ·

How to iterate over a range with a non-standard step in Rust?

Hello, colleagues!\n\nI recently started learning Rust and encountered a task that seems simple, but I can't find an elegant solution. I need to iterate over numbers from 0 to 100, but not consecutively, with a step of 5. That is: 0, 5, 10, 15, ..., 100.\n\nIn Python, I would write something like `for i in range(0, 101, 5)`, and it works. In Rust, I know about `for i in 0..=100`, but that gives all numbers consecutively. I tried using `.step_by()`, but the compiler complains that ranges don't have such a method. Then I googled some `Step` trait, but it's either unstable or something else.\n\nHere's what I tried to do:\n\n```rust\nfn main() {\n for i in 0..=100.step_by(5) {\n println!("{}", i);\n }\n}\n```\n\nThe compiler gives an error: `no method named 'step_by' found for struct 'RangeInclusiv`