Rust loops: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 28: | Line 28: | ||
= | = loops = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
Line 37: | Line 37: | ||
</blockquote><!-- loop --> | </blockquote><!-- loop --> | ||
= while = | = while loops = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="rust"> | <syntaxhighlight lang="rust"> | ||
Line 47: | Line 47: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- while --> | </blockquote><!-- while --> | ||
= for loops = | |||
<blockquote> | |||
<syntaxhighlight lang="rust"> | |||
let cats = ["boulie", "icarus", "lucifer"]; | |||
for cat in cats { | |||
println!("{cat}"); | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- for loops --> |
Latest revision as of 14:50, 7 February 2023
General
Breaking/Continuing etc.
break; // exit loop continue; // skip to next iteration of loopBreak can be assigned a return value
break 123 // return value '123' from loopLoop Labels
Loops can be assigned labels, so that you can target which loop to break out of.
'outer_loop: loop { loop { break 'outer_loop; } }
loops
loop { // loop forever }
while loops
let mut num = 0; while num < 5 { index += 1; }
for loops
let cats = ["boulie", "icarus", "lucifer"]; for cat in cats { println!("{cat}"); }