diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
commit | 27514d58573ce1f844af4ea60afa72c7f58f1cd7 (patch) | |
tree | 4b16c31f73cb72435d03d5e105261143f8ca135f /The option enum.md | |
parent | 22675cd8dc75d8b8d4b0f818f5b093efbc364802 (diff) |
Diffstat (limited to 'The option enum.md')
-rw-r--r-- | The option enum.md | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/The option enum.md b/The option enum.md new file mode 100644 index 0000000..3534ea0 --- /dev/null +++ b/The option enum.md @@ -0,0 +1,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.
\ No newline at end of file |