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

前端 未结 6 917
不思量自难忘°
不思量自难忘° 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:26

    There is way using let "redefinition":

    for i in 0..((n + 1) / 2) {
        let i = i * 2;
        // …
    }
    

    Or use Iterator::map:

    for i in (0..((n + 1) / 2)).map(|i| i * 2) {
        // …
    }
    

提交回复
热议问题