summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_error_codes/src/error_codes/E0317.md
blob: 230911c208628fcd79616a094481721f61d54aee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
An `if` expression is missing an `else` block.

Erroneous code example:

```compile_fail,E0317
let x = 5;
let a = if x == 5 {
    1
};
```

This error occurs when an `if` expression without an `else` block is used in a
context where a type other than `()` is expected. In the previous code example,
the `let` expression was expecting a value but since there was no `else`, no
value was returned.

An `if` expression without an `else` block has the type `()`, so this is a type
error. To resolve it, add an `else` block having the same type as the `if`
block.

So to fix the previous code example:

```
let x = 5;
let a = if x == 5 {
    1
} else {
    2
};
```