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

前端 未结 6 916
不思量自难忘°
不思量自难忘° 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条回答
  •  星月不相逢
    2021-01-01 19:38

    Use the crate num

    Cargo.toml:

    [dependencies.num]
    version = "0.1.25"
    default-features = false
    

    Since you only need the crate's basics, use default-features = false.

    Rust:

    extern crate num;
    
    use num::range_step;
    
    for i in range_step(0, 10, 2) {
        /*    */
    }
    

    range_step is generic over rust's integer types.

提交回复
热议问题