我如何用1以外的步长迭代 rust 蚀范围?我来自C++背景,所以我想做点什么
for(auto i = 0; i <= n; i+=2) {
//...
}
在Rust中,我需要使用range
函数,而且似乎没有第三个参数可用于自定义步骤.我怎样才能做到这一点?
我如何用1以外的步长迭代 rust 蚀范围?我来自C++背景,所以我想做点什么
for(auto i = 0; i <= n; i+=2) {
//...
}
在Rust中,我需要使用range
函数,而且似乎没有第三个参数可用于自定义步骤.我怎样才能做到这一点?
range_step_inclusive
和range_step
早就消失了.
在Rust 1.28中,Iterator::step_by
是稳定的:
fn main() {
for x in (1..10).step_by(2) {
println!("{}", x);
}
}