Rust Control Flow

Rust control flow

In Rust, control flow refers to the ability to run code based on a condition or set of conditions.

Control flow is useful whenever we want a block of code to run when a condition is met. We may want the code to run one time, or we may want it to run multiple times or until some other condition is met.

Control flow is also a broad term in computer science that can be defined as the order in which code is executed.

Rust provides tools that allow control flow and are generally similar to the same utilities from other programming languages. These include conditional expressions like if as well as for and while loops.

There are two common types of control flow statements: conditional expressions, and loops.

Conditional expressions include if…else and match. Loops include for, while, and loop.

Conditional Expressions

Conditional expressions run a block of code if a specific condition is met.

Conditional expressions include:

  • if expressions, including else, if else, and nested if expressions
  • if let
  • match

Loops

Loops include:

  • for
  • while