---
title: "Functions and Control Flow | Bitterless Rust"
description: "fn, if, match, for, and the expression-based world"
last_updated: "2026-11-15"
canonical_url: "https://wtrclred.vercel.app/en/books/bitterless-rust/04"
language: "en"
---

# Functions and Control Flow | Bitterless Rust

Turn on watercolor background effects. * May affect performance, battery life, and temperature on some devices.

### Table of Contents

- [Functions](#functions)
- [Functions That Return Nothing](#functions-that-return-nothing)
- [if / else](#if-else)
- [if Is an Expression](#if-is-an-expression)
- [match](#match)
- [for Loop](#for-loop)
- [while Loop](#while-loop)
- [loop (Infinite Loop)](#loop-infinite-loop)

## Functions

 Define functions with `fn`. Arguments and return types need type annotations:

 

```rust
  fn   add  (  a  :   i32  ,   b  :   i32  )   -  >   i32   { 
     a   +   b 
 } 

 fn   main  (  )   { 
     let   result   =   add  (  1  ,   2  )  ; 
     println  !  (  "{}"  ,   result  )  ;   // 3 
 }
```

 Did you notice — there's no `return` in `add`. In Rust, the last expression becomes the return value . Be careful: adding a semicolon `;` turns it into a "statement" and it stops returning a value:

 

```rust
  fn   add  (  a  :   i32  ,   b  :   i32  )   -  >   i32   { 
     a   +   b     // ← no semicolon = returns this value 
 } 

 fn   add_wrong  (  a  :   i32  ,   b  :   i32  )   -  >   i32   { 
     a   +   b  ;    // ← semicolon = doesn't return a value = compile error 
 }
```

 You can also use `return` to return explicitly:

 

```rust
  fn   add  (  a  :   i32  ,   b  :   i32  )   -  >   i32   { 
     return   a   +   b  ; 
 }
```

 Use `return` when you want to exit early. When the last expression can do the job, writing it without a semicolon is the Rust way.

## Functions That Return Nothing

 Functions that don't return anything omit the `->`:

 

```rust
  fn   greet  (  name  :   String  )   { 
     println  !  (  "Hello, {}!"  ,   name  )  ; 
 }
```

## if / else

 

```rust
  fn   main  (  )   { 
     let   x   =   42  ; 

     if   x   >   0   { 
         println  !  (  "positive"  )  ; 
     }   else   if   x   <   0   { 
         println  !  (  "negative"  )  ; 
     }   else   { 
         println  !  (  "zero"  )  ; 
     } 
 }
```

 No `()` needed around the condition. The compiler will tell you they're unnecessary if you add them.

### if Is an Expression

 Rust's `if` is an expression, so it can return a value:

 

```rust
  fn   main  (  )   { 
     let   x   =   42  ; 
     let   label   =   if   x   >   0   {   "positive"   }   else   {   "negative"   }  ; 
     println  !  (  "{}"  ,   label  )  ; 
 }
```

 Use it instead of the ternary operator (`? :`).

## match

 `match` is pattern matching. A powered-up version of `switch` from other languages:

 

```rust
  fn   main  (  )   { 
     let   x   =   2  ; 

     match   x   { 
         1   =  >   println  !  (  "one"  )  , 
         2   =  >   println  !  (  "two"  )  , 
         3   =  >   println  !  (  "three"  )  , 
         _   =  >   println  !  (  "other"  )  ,    // _ matches everything else 
     } 
 }
```

 `_` is the default case (`switch`'s `default`).

 `match` is also an expression, so it can return a value:

 

```rust
  fn   main  (  )   { 
     let   x   =   2  ; 

     let   name   =   match   x   { 
         1   =  >   "one"  , 
         2   =  >   "two"  , 
         _   =  >   "other"  , 
     }  ; 

     println  !  (  "{}"  ,   name  )  ;   // two 
 }
```

 `match` really shines when combined with enums in later chapters.

## for Loop

 

```rust
  fn   main  (  )   { 
     // 0 to 4 
     for   i   in   0  .  .  5   { 
         println  !  (  "{}"  ,   i  )  ; 
     } 

     // Looping over an array (Vec) 
     let   names   =   vec  !  [  "Alice"  ,   "Bob"  ,   "Charlie"  ]  ; 
     for   name   in   names   { 
         println  !  (  "Hello, {}!"  ,   name  )  ; 
     } 
 }
```

 `0..5` is a range meaning "0 or more, less than 5." `0..=5` means "0 or more, 5 or less."

> vec![...] is a macro that creates a Vec (dynamic array). More on this in a later chapter.

## while Loop

 

```rust
  fn   main  (  )   { 
     let   mut   count   =   0  ; 

     while   count   <   5   { 
         println  !  (  "{}"  ,   count  )  ; 
         count   +  =   1  ; 
     } 
 }
```

## loop (Infinite Loop)

 

```rust
  fn   main  (  )   { 
     let   mut   count   =   0  ; 

     loop   { 
         if   count   >  =   5   { 
             break  ; 
         } 
         println  !  (  "{}"  ,   count  )  ; 
         count   +  =   1  ; 
     } 
 }
```

 Use `break` to exit. `loop` can also be used as an expression:

 

```rust
  fn   main  (  )   { 
     let   mut   count   =   0  ; 

     let   final_count   =   loop   { 
         count   +  =   1  ; 
         if   count   >  =   10   { 
             break   count  ;    // passing a value to break makes it the loop's return value 
         } 
     }  ; 

     println  !  (  "{}"  ,   final_count  )  ;   // 10 
 }
```

 Functions and control flow are done. Next, let's learn structs and enums to create our own data types.

 [← Previous Variables and Types](/en/books/bitterless-rust/03)

 [Next → Structs and Enums](/en/books/bitterless-rust/05)

[← Back to book](/en/books/bitterless-rust)

## Sitemap

- [Browse every page on wtrclred](https://wtrclred.vercel.app/sitemap.md)
