From 27514d58573ce1f844af4ea60afa72c7f58f1cd7 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Thu, 21 Aug 2025 22:09:29 +0200 Subject: vault backup: 2025-08-21 22:09:29 --- The option enum.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 The option enum.md (limited to 'The option enum.md') 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 { + None + Some(T) +} +``` + +This enables the compiler to check whether we handle both cases. Why? +```rust +fn do_something(int: i32, nope: bool) -> Option { + if (nope) { + return None + } + + return Some(int * 2) +} +``` + +The concrete return type is either `None` or `Some`: 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 -- cgit v1.2.3