3.5. if
if
Rust’s take on if
is not particularly complex, but it’s much more like the if
you’ll find in a dynamically typed language than in a more traditional systems language. So let’s talk about it, to make sure you grasp the nuances.
if
is a specific form of a more general concept, the ‘branch’, whose name comes from a branch in a tree: a decision point, where depending on a choice, multiple paths can be taken.
In the case of if
, there is one choice that leads down two paths:
# #![allow(unused_variables)] #fn main() { let x = 5; if x == 5 { println!("x is five!"); } #}
If we changed the value of x
to something else, this line would not print. More specifically, if the expression after the if
evaluates to true
, then the block is executed. If it’s false
, then it is not.
If you want something to happen in th