if expressions

You use if very similarly to how you would in other languages:

  1. fn main() {
  2. let mut x = 10;
  3. if x % 2 == 0 {
  4. x = x / 2;
  5. } else {
  6. x = 3 * x + 1;
  7. }
  8. }

In addition, you can use it as an expression. This does the same as above:

  1. fn main() {
  2. let mut x = 10;
  3. x = if x % 2 == 0 {
  4. x / 2
  5. } else {
  6. 3 * x + 1
  7. };
  8. }

Because if is an expression and must have a particular type, both of its branch blocks must have the same type. Consider showing what happens if you add ; after x / 2 in the second example.