Search⌘ K

Evaluating to Values

Explore how if expressions in Rust evaluate to values by requiring all branches to return the same type. Understand how to assign the result of if expressions to variables and use them directly in code, improving conditional logic and code readability.

We'll cover the following...

I mentioned that an if is an expression. And if you remember from Anatomy of Rust:

An expression is evaluated to a value of a specific type

So far, every if expression we’ve had evaluates to unit. However, we can have it evaluate to something else, with the following restriction: ...