mirror of
https://codeberg.org/ziglings/exercises.git
synced 2024-12-26 01:20:30 +00:00
Merge pull request '046: Show usage of .?
and hint towards new solution.' (#116) from alexmchughnz/ziglings:fix046 into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/116
This commit is contained in:
commit
f496d79ab9
2 changed files with 27 additions and 12 deletions
|
@ -5,7 +5,7 @@
|
||||||
// linked to the first elephant. This is because we had NO CONCEPT
|
// linked to the first elephant. This is because we had NO CONCEPT
|
||||||
// of a tail that didn't point to another elephant!
|
// of a tail that didn't point to another elephant!
|
||||||
//
|
//
|
||||||
// We also introduce the handy ".?" shortcut:
|
// We also introduce the handy `.?` shortcut:
|
||||||
//
|
//
|
||||||
// const foo = bar.?;
|
// const foo = bar.?;
|
||||||
//
|
//
|
||||||
|
@ -13,7 +13,8 @@
|
||||||
//
|
//
|
||||||
// const foo = bar orelse unreachable;
|
// const foo = bar orelse unreachable;
|
||||||
//
|
//
|
||||||
// See if you can find where we use this shortcut below.
|
// Check out where we use this shortcut below to change control flow
|
||||||
|
// based on if an optional value exists.
|
||||||
//
|
//
|
||||||
// Now let's make those elephant tails optional!
|
// Now let's make those elephant tails optional!
|
||||||
//
|
//
|
||||||
|
@ -31,14 +32,25 @@ pub fn main() void {
|
||||||
var elephantC = Elephant{ .letter = 'C' };
|
var elephantC = Elephant{ .letter = 'C' };
|
||||||
|
|
||||||
// Link the elephants so that each tail "points" to the next.
|
// Link the elephants so that each tail "points" to the next.
|
||||||
elephantA.tail = &elephantB;
|
linkElephants(&elephantA, &elephantB);
|
||||||
elephantB.tail = &elephantC;
|
linkElephants(&elephantB, &elephantC);
|
||||||
|
|
||||||
|
// `linkElephants` will stop the program if you try and link an
|
||||||
|
// elephant that doesn't exist! Uncomment and see what happens.
|
||||||
|
// const missingElephant: ?*Elephant = null;
|
||||||
|
// linkElephants(&elephantC, missingElephant);
|
||||||
|
|
||||||
visitElephants(&elephantA);
|
visitElephants(&elephantA);
|
||||||
|
|
||||||
std.debug.print("\n", .{});
|
std.debug.print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If e1 and e2 are valid pointers to elephants,
|
||||||
|
// this function links the elephants so that e1's tail "points" to e2.
|
||||||
|
fn linkElephants(e1: ?*Elephant, e2: ?*Elephant) void {
|
||||||
|
e1.?.*.tail = e2.?;
|
||||||
|
}
|
||||||
|
|
||||||
// This function visits all elephants once, starting with the
|
// This function visits all elephants once, starting with the
|
||||||
// first elephant and following the tails to the next elephant.
|
// first elephant and following the tails to the next elephant.
|
||||||
fn visitElephants(first_elephant: *Elephant) void {
|
fn visitElephants(first_elephant: *Elephant) void {
|
||||||
|
@ -51,6 +63,9 @@ 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?
|
||||||
|
|
||||||
|
// HINT: We want something similar to what `.?` does,
|
||||||
|
// but instead of ending the program, we want to exit the loop...
|
||||||
e = e.tail ???
|
e = e.tail ???
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
--- exercises/046_optionals2.zig 2024-05-10 23:11:25.796632478 +0200
|
--- exercises/046_optionals2.zig 2024-06-23 19:43:16
|
||||||
+++ answers/046_optionals2.zig 2024-05-10 23:10:16.115335668 +0200
|
+++ answers/046_optionals2.zig 2024-06-23 19:42:46
|
||||||
@@ -21,7 +21,7 @@
|
@@ -22,7 +22,7 @@
|
||||||
|
|
||||||
const Elephant = struct {
|
const Elephant = struct {
|
||||||
letter: u8,
|
letter: u8,
|
||||||
- tail: *Elephant = null, // Hmm... tail needs something...
|
- tail: *Elephant = null, // Hmm... tail needs something...
|
||||||
+ tail: ?*Elephant = null, // <---- make this optional!
|
+ tail: ?*Elephant = null, // Hmm... tail needs something...
|
||||||
visited: bool = false,
|
visited: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -51,6 +51,6 @@
|
@@ -66,6 +66,6 @@
|
||||||
// We should stop once we encounter a tail that
|
|
||||||
// does NOT point to another element. What can
|
// HINT: We want something similar to what `.?` does,
|
||||||
// we put here to make that happen?
|
// but instead of ending the program, we want to exit the loop...
|
||||||
- e = e.tail ???
|
- e = e.tail ???
|
||||||
+ e = e.tail orelse break;
|
+ e = e.tail orelse break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue