What is a stable way to iterate on a range with custom step?

前端 未结 6 902
不思量自难忘°
不思量自难忘° 2021-01-01 18:41

How should I go if I want to iterate with a custom step in stable Rust? Essentially something like the C/C++

for (in         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 19:13

    Since this question was asked, the itertools crate, has become fairly standard as a dependency. You can do what you want very simply with the step() method:

    extern crate itertools; // 0.7.8
    use itertools::Itertools;
    
    fn main() {
        for i in (0..=10).step(2) {
            println!("i = {}", i);
        }
    }
    

    In your Cargo.toml:

    [dependencies]
    itertools = "0.7.8"
    

提交回复
热议问题