optionals done

This commit is contained in:
2026-06-21 20:52:47 +02:00
parent 28a82c5c08
commit a5bc189274
2 changed files with 6 additions and 3 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ pub fn main() void {
// Please threaten the result so that answer is either the // Please threaten the result so that answer is either the
// integer value from deepThought() OR the number 42: // integer value from deepThought() OR the number 42:
const answer: u8 = result; const answer: u8 = result orelse 42;
std.debug.print("The Ultimate Answer: {}.\n", .{answer}); std.debug.print("The Ultimate Answer: {}.\n", .{answer});
} }
+5 -2
View File
@@ -22,7 +22,7 @@ const std = @import("std");
const Elephant = struct { const Elephant = struct {
letter: u8, letter: u8,
tail: *Elephant = null, // Hmm... tail needs something... tail: ?*Elephant = null, // Hmm... tail needs something...
visited: bool = false, visited: bool = false,
}; };
@@ -63,9 +63,12 @@ fn visitElephants(first_elephant: *Elephant) void {
// We should stop once we encounter a tail that // We should stop once we encounter a tail that
// does NOT point to another element. What can // does NOT point to another element. What can
// we put here to make that happen? // we put here to make that happen?
if (e.tail == null) {
break;
}
// HINT: We want something similar to what `.?` does, // HINT: We want something similar to what `.?` does,
// but instead of ending the program, we want to exit the loop... // but instead of ending the program, we want to exit the loop...
e = e.tail ??? e = e.tail.?;
} }
} }