Control Flow

1

Control Flow: if, while, loop, and for

6 minute

C programmers usually start with this mapping:

C if       -> Rust if
C while    -> Rust while
C for      -> Rust for
C break    -> Rust break
C continue -> Rust continue

The mapping is mostly right, but a few differences affect everyday code:

  • if and while conditions must be bool
  • if can produce a value
  • Rust has no C-style for (init; condition; step)
  • Rust for iterates over an iterable value
  • loop expresses an intentional infinite loop and can return a value with break value

These are not advanced features. They show up immediately in CLI tools and buffer processing.

Read More