% while Loops while循环
Rust also has a while
loop. It looks like this:
Rust同样有while
循环,它看起来这样:
let mut x = 5; // mut x: i32
let mut done = false; // mut done: bool
while !done {
x += x - 3;
println!("{}", x);
if x % 5 == 0 {
done = true;
}
}
while
loops are the correct choice when you’re not sure how many times you need to loop.
while
循环是当你不确定你需要循环多少次时的正确的选择。
If you need an infinite loop, you may be tempted to write this:
如果你需要无限循环,你可能视图这样写:
while true {
However, Rust has a dedicated keyword, loop
, to handle this case:
然而,Rust有一个用来处理这个案例的专门的关键词,loop
:
loop {
Rust’s control-flow analysis treats this construct differently than a while true
, since we know that it will always loop. In general, the more information we can give to the compiler, the better it can do with safety and code generation, so you should always prefer loop
when you plan to loop
infinitely.
Rust的控制流分析系统对待这个结构不同于while true
,因为我们知道它总是循环。通常情况下,我们能够给到编译器的信息越多,编译器安全操作和代码生成就做的越好,所以,当你计划使用无限循环时,你应该总是偏爱loop
。
Let’s take a look at that while
loop we had earlier:
让我们看一下前面已经有的while
循环:
let mut x = 5;
let mut done = false;
while !done {
x += x - 3;
println!("{}", x);
if x % 5 == 0 {
done = true;
}
}
We had to keep a dedicated mut
boolean variable binding, done
, to know when we should exit out of the loop. Rust has two keywords to help us with modifying iteration: break
and continue
.
我们不得不保持一个专门的mut
布尔变量绑定,done
,来确认什么时候我们应该跳出循环。Rist有两个关键词来帮助我们修改迭代:break
和continue
。
In this case, we can write the loop in a better way with break
:
在这个案例中,我们可以使用break
以一种更好的方式写这个循环:
let mut x = 5;
loop {
x += x - 3;
println!("{}", x);
if x % 5 == 0 { break; }
}
We now loop forever with loop
and use break
to break out early.
我们可以使用loop
永远循环,并且使用break
来及早打断跳出循环。
continue
is similar, but instead of ending the loop, goes to the next iteration. This will only print the odd numbers:
continu
类似,然而不同于结束循环,它会进入到下次迭代中。这将只打印偶数:
for x in 0..10 {
if x % 2 == 0 { continue; }
println!("{}", x);
}
Both continue
and break
are valid in both while
loops and for
loops.
continue
和break
对while
循环和for
循环同样有效。
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )