问题
Assuming I want a finite loop using a range:
let mut x: i32 = 0;
for i in 1..10 {
x += 1;
}
The compiler will spit out the warning:
warning: unused variable: `i`, #[warn(unused_variables)] on by default
for i in 1..10 {
^
Is there a more idiomatic way to write this that won't make the compiler complain?
回答1:
You can write _
as your pattern, meaning “discard the value”:
let mut x: i32 = 0;
for _ in 1..10 {
x += 1;
}
来源:https://stackoverflow.com/questions/29932503/what-is-the-idiomatic-way-to-write-a-for-loop-without-using-the-iterator-value