Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The if statement allows conditional branching of code.

The expression used as the condition needs to evaluate to a boolean value. If not, the compiler issues an error:

[E0308] Error: mismatched types
   ╭─[ command_2:1:1 ]
   │
 1 │ if 1 {
   │    ┬  
   │    ╰── expected `bool`, found integer
───╯

Here is a correct conditional expression:

True
()

Example of chained if/else:

No
()

10.1.1As an expression

if/else statement can be used as an expression:

-4.1

We can chain the if/else statements:

(3, 4)