blob: 3534ea0e0a730baff46ec2623dc0c3d14ac44338 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Is in the prelude, so always available. Used where we can return something or none, instead of null.
```rust
enum Option<T> {
None
Some(T)
}
```
This enables the compiler to check whether we handle both cases. Why?
```rust
fn do_something(int: i32, nope: bool) -> Option<i32> {
if (nope) {
return None
}
return Some(int * 2)
}
```
The concrete return type is either `None` or `Some<i32>`: so if the called would for example do `1 + do_something(2, false);` would not compile because the return type is not an i32. Unlike for example PHP where it's `?int` so either `null` or `int` the compiler can't check this.
|