在不使用迭代器值的情况下编写 for 循环的惯用方法是什么?

What is the idiomatic way to write a for loop without using the iterator value?

假设我想要一个使用范围的有限循环:

let mut x: i32 = 0;
for i in 1..10 {
    x += 1;
}

编译器会吐出警告:

warning: unused variable: `i`, #[warn(unused_variables)] on by default
for i in 1..10 {
    ^

有没有更惯用的方式来编写这个不会让编译器抱怨的方法?

你可以写_作为你的模式,意思是“丢弃价值”:

let mut x: i32 = 0;
for _ in 1..10 {
    x += 1;
}