Click a card to flip it · 16 terms
Control flow determines which code runs and when. This deck covers if, all three loop types, match for pattern matching, and if let for simpler one-pattern matches.
A conditional expression. In Rust, if returns a value, so both branches must have the same type.
Choosing between two paths based on a condition.
let label = if score > 90 { "A"} else { "B"};
Repeats code for each item in an iterator or range.
Visiting every element in a collection. The standard loop in Rust.
for name in &names { println!("{}", name);}
Repeats code as long as a condition stays true.
When you don't know how many times to loop. Runs until the condition becomes false.
while count < 10 { count += 1;}
An intentional infinite loop that runs until a break statement exits it.
When you need full control over when to stop. Break can also return a value.
let result = loop { if done { break 42; }};
Immediately exits the current loop.
When a condition is met and you want to stop all remaining iterations.
if amount == 0 { break;}
Skips the rest of the current iteration and jumps back to the top of the loop.
Skipping bad or empty entries without stopping the whole loop.
if amount == 0 { continue; // skip this one}
A name given to a loop ('outer:) so break 'outer can exit that specific loop.
Required when nested loops exist and you need to exit an outer one from inside.
'outer: for x in 0..10 { for y in 0..10 { if x == y { break 'outer; } }}
Generates a sequence from the start up to but NOT including the end.
When the boundary itself should not be included in the loop.
for i in 1..10 { // 1 through 9, not 10}
Like .. but the end number IS included.
When the boundary itself must be covered.
for i in 1..=10 { // 1 through 10, included}
Compares a value against multiple patterns. Compiler enforces completeness.
Cleaner than long if/else chains. Required for enums and Option.
match coin { Coin::Penny => 1, Coin::Quarter => 25, _ => 0,}
A description of what a value should look like. Written on the left side of => in a match arm.
Matching on literals, variables, enum variants, structs, or wildcards.
match x { 1 => "one", // pattern _ => "other", // wildcard}
A one-pattern shortcut for match. Handles one case and ignores everything else.
When only one variant matters and a full match would be verbose.
if let Some(val) = result { println!("{}", val);}
A catch-all arm in match that matches anything not covered by earlier arms.
Always include this to make a match exhaustive when not all cases are listed.
match direction { North => go_up(), _ => println!("other"),}
Every possible value must be covered in a match. The compiler enforces this.
If you forget a variant, your code won't compile. Rust catches this for you.
// forgetting a variant// = compile error// add _ => {} to catch the rest
A second (or third) condition to check when the first if was false.
When there are more than two possible paths.
if x > 90 { "A" }else if x > 75 { "B" }else { "C" }
Wraps each element with its index, returning (index, value) tuples.
When you need both the position and the value in a for loop.
for (i, name) in names.iter().enumerate() { println!("{}: {}", i, name);}