Hi ππ» , I'm trying to complete Zigling 46. I've changed the type of 'tail' in the struct to be an optional pointer. My understanding is that for elephantC the tail will be null and by using e.tail.? it should jump into 'unreachable' and do nothing.
Could someone please point out where I'm going wrong?
**The output: **
Compiling: 046_optionals2.zig
Checking: 046_optionals2.zig
error: 046_optionals2.zig terminated unexpectedly
My Zig version:
zig version 0.13.0
The code
`const std = @import("std");
const Elephant = struct {
letter: u8,
tail: ?*Elephant = null, // Hmm... tail needs something...
visited: bool = false,
};
pub fn main() void {
var elephantA = Elephant{ .letter = 'A' };
var elephantB = Elephant{ .letter = 'B' };
var elephantC = Elephant{ .letter = 'C' };
// Link the elephants so that each tail "points" to the next.
elephantA.tail = &elephantB;
elephantB.tail = &elephantC;
visitElephants(&elephantA);
std.debug.print("\n", .{});
}
// This function visits all elephants once, starting with the
// first elephant and following the tails to the next elephant.
fn visitElephants(first_elephant: *Elephant) void {
var e = first_elephant;
while (!e.visited) {
std.debug.print("Elephant {u}. ", .{e.letter});
e.visited = true;
// We should stop once we encounter a tail that
// does NOT point to another element. What can
// we put here to make that happen?
e = e.tail.?;
}
}`