if
expressionsloop
andwhile
loopsmatch
expressionsfor
loopsbreak
andcontinue
return
and?
- Tests if a boolean expression is
true
- Parentheses around the conditional are not necessary
- Blocks need brackets, no shorthand
fn main() {
if 1 == 2 {
println!("integers are broken");
} else if 'a' == 'b' {
println!("characters are broken");
} else {
println!("that's what I thought");
}
}
- Every block is an expression
- Note the final
;
to terminate thelet
statement.
fn main() {
let x = if 1 == 2 {
100
} else if 'a' == 'b' {
200
} else {
300
};
}
Now the if
expression is the result of the function:
fn some_function() -> i32 {
if 1 == 2 {
100
} else if 'a' == 'b' {
200
} else {
300
}
}
loop
is used for (potentially) infinite loops
fn main() {
let mut i = 0;
loop {
i += 1;
if i > 100 { break; }
}
}
loop
blocks are also expressions...
fn main() {
let mut i = 0;
let loop_result = loop {
i += 1;
if i > 10 { break 6; }
println!("i = {}", i);
};
println!("loop_result = {}", loop_result);
}
while
is used for conditional loops.- Loops while the boolean expression is
true
fn main() {
let mut i = 0;
while i < 10 {
i += 1;
println!("i = {}", i);
}
}
- The
match
keyword does pattern matching - You can use it a bit like an
if/else if/else
expression - The first arm to match, wins
_
means match anything
fn main() {
let a = 4;
match a % 3 {
0 => { println!("divisible by 3") }
_ => { println!("not divisible by 3") }
}
}
for
is used for iteration- Here
0..10
creates aRange
, which you can iterate
fn main() {
for num in 0..10 {
println!("{}", num);
}
}
Lots of things are iterable
fn main() {
for ch in "Hello".chars() {
println!("{}", ch);
}
}
- What Rust actually does is more like...
- (More on this in the section on Iterators)
fn main() {
let mut iter = "Hello".chars().into_iter();
loop {
match iter.next() {
Some(ch) => println!("{}", ch),
None => break,
}
}
}
If you have nested loops, you can label them to indicate which one you want to break out of.
fn main() {
'cols: for x in 0..5 {
'rows: for y in 0..5 {
println!("x = {}, y = {}", x, y);
if x + y >= 6 {
break 'cols;
}
}
}
}
Means go around the loop again, rather than break out of the loop
fn main() {
'cols: for x in 0..5 {
'rows: for y in 0..5 {
println!("x = {}, y = {}", x, y);
if x + y >= 4 {
continue 'cols;
}
}
}
}
return
can be used for early returns- The result of the last expression of a function is always returned
fn get_number(x: bool) -> i32 {
if x {
return 42;
}
-1
}