What is Control Flow in Rust?

Author: Mansoor Ahmed
  • A control flow is the order in which the piece of program is executed.

  • In control flow we have following expressions:

  1. If expression
  2. Else expression
  3. Else if expression

If Expression:

  • If expression allows to branch your code to any condition.

  • If the condition is true then the program in if block will be executed.

  • When if expression is not true then the program will be passed to further blocks of else if expressions or else expression.

  • The curly brackets defining the program blocks are called arms.

Else Expression:

  • Else expression gives the program an alternative block of code to execute when the condition introduced in if block evaluates to false.

  • Else expression comes at last.

Example

fn main() {

let number = 3;

if number