How to make a reverse ordered for loop in Rust?

后端 未结 2 1647
迷失自我
迷失自我 2020-12-25 09:25

Editor\'s note: This question was asked before Rust 1.0 was released and the .. \"range\" operator was introduced. The question\'s code no longer

2条回答
  •  鱼传尺愫
    2020-12-25 10:14

    A forward loop is like this:

    for x in 0..100 {
        println!("{}", x);
    }
    

    And a reverse loop is done by calling Iterator::rev to reverse the order:

    for x in (0..100).rev() {
        println!("{}", x);
    }
    

提交回复
热议问题