mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
Merge pull request 'Add another defer exercise' (#438) from mark2185/exercises:defer3 into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/438
This commit is contained in:
@@ -10,6 +10,8 @@ pub fn main() void {
|
|||||||
for (animals) |a| printAnimal(a);
|
for (animals) |a| printAnimal(a);
|
||||||
|
|
||||||
std.debug.print("done.\n", .{});
|
std.debug.print("done.\n", .{});
|
||||||
|
|
||||||
|
std.debug.print("Answer to everything? {d}\n", .{calculateTheUltimateQuestionOfLife()});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is _supposed_ to print an animal name in parentheses
|
// This function is _supposed_ to print an animal name in parentheses
|
||||||
@@ -35,3 +37,24 @@ fn printAnimal(animal: u8) void {
|
|||||||
|
|
||||||
std.debug.print("Unknown", .{});
|
std.debug.print("Unknown", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function is supposed to calculate the answer to the
|
||||||
|
// ultimate question of life, the universe, and everything,
|
||||||
|
// but it needs to be deferred as far in the future as possible,
|
||||||
|
// in order to gather more data.
|
||||||
|
//
|
||||||
|
// When there are multiple defers in a single block, they are executed in reverse order.
|
||||||
|
// This example might seem silly, but it's important to know when e.g.
|
||||||
|
// deinitializing containers whose elements need to be deinitialized first.
|
||||||
|
fn calculateTheUltimateQuestionOfLife() u32 {
|
||||||
|
var x: u32 = 100;
|
||||||
|
|
||||||
|
// Try reordering the statements to get the answer 42
|
||||||
|
{
|
||||||
|
defer x = x / 10;
|
||||||
|
defer x = x + 11;
|
||||||
|
defer x = x * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
--- exercises/028_defer2.zig 2023-10-03 22:15:22.122241138 +0200
|
--- exercises/028_defer2.zig 2026-06-02 06:08:12.713672612 +0200
|
||||||
+++ answers/028_defer2.zig 2023-10-05 20:04:06.966098530 +0200
|
+++ answers/028_defer2.zig 2026-06-02 06:08:43.262234023 +0200
|
||||||
@@ -18,7 +18,7 @@
|
@@ -20,7 +20,7 @@
|
||||||
fn printAnimal(animal: u8) void {
|
fn printAnimal(animal: u8) void {
|
||||||
std.debug.print("(", .{});
|
std.debug.print("(", .{});
|
||||||
|
|
||||||
@@ -9,3 +9,15 @@
|
|||||||
|
|
||||||
if (animal == 'g') {
|
if (animal == 'g') {
|
||||||
std.debug.print("Goat", .{});
|
std.debug.print("Goat", .{});
|
||||||
|
@@ -51,9 +51,9 @@
|
||||||
|
|
||||||
|
// Try reordering the statements to get the answer 42
|
||||||
|
{
|
||||||
|
- defer x = x / 10;
|
||||||
|
- defer x = x + 11;
|
||||||
|
defer x = x * 2;
|
||||||
|
+ defer x = x + 11;
|
||||||
|
+ defer x = x / 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
|||||||
@@ -693,7 +693,10 @@ const exercises = [_]Exercise{
|
|||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "028_defer2.zig",
|
.main_file = "028_defer2.zig",
|
||||||
.output = "(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done.",
|
.output =
|
||||||
|
\\(Goat) (Cat) (Dog) (Dog) (Goat) (Unknown) done.
|
||||||
|
\\Answer to everything? 42
|
||||||
|
, // pay attention to the comma
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "029_errdefer.zig",
|
.main_file = "029_errdefer.zig",
|
||||||
|
|||||||
Reference in New Issue
Block a user