在不使用迭代器值的情况下编写 for 循环的惯用方法是什么? [英] What is the idiomatic way to write a for loop without using the iterator value?

查看:30
本文介绍了在不使用迭代器值的情况下编写 for 循环的惯用方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assuming I want a finite loop using a range:

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 {
    ^

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

Is there a more idiomatic way to write this that won't make the compiler complain?

推荐答案

你可以将 _ 写成你的模式,意思是丢弃值":

You can write _ as your pattern, meaning "discard the value":

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

这篇关于在不使用迭代器值的情况下编写 for 循环的惯用方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆