summaryrefslogtreecommitdiff
path: root/The option enum.md
diff options
context:
space:
mode:
Diffstat (limited to 'The option enum.md')
-rw-r--r--The option enum.md21
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