Rust while 条件循环
Rust 内置了while循环结构;当条件为真时,执行循环,当条件不为真时,停止循环;
示例代码
完成1到100的累加:
fn main() {
let mut number = 100;
let mut result = 0;
while number != 0 {
result += number;
number -= 1;
}
println!("{result}");
}
运行结果
5050
使用while循环遍历数组:
fn main() {
let array = [10, 20, 30, 40, 50];
let length = array.len(); //获取数组长度
let mut index = 0;
while index < length {
println!("{}", array[index]);
index += 1;
}
}
运行结果
10
20
30
40
50
如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!
txttool.com 说一段 esp56物联 查询128 IP查询