From e61bedaa25e4bc80847c4fb1508b5f4cdf35cd20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Sun, 31 May 2026 22:26:17 +0200 Subject: [PATCH 1/2] Add another defer exercise --- exercises/116_defer3.zig | 18 ++++++++++++++++++ patches/patches/116_defer3.patch | 14 ++++++++++++++ src/elrond.zig | 4 ++++ 3 files changed, 36 insertions(+) create mode 100644 exercises/116_defer3.zig create mode 100644 patches/patches/116_defer3.patch diff --git a/exercises/116_defer3.zig b/exercises/116_defer3.zig new file mode 100644 index 0000000..6e3cf6d --- /dev/null +++ b/exercises/116_defer3.zig @@ -0,0 +1,18 @@ +// +// When there are multiple defers in a single block, they are executed in reverse order. +// +const std = @import("std"); + +pub fn main() void { + 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; + + // It might seem silly in this example, but it's important to know when + // deinitializing containers whose elements need to be deinitialized first. + } + std.debug.print("{d}\n", .{x}); +} diff --git a/patches/patches/116_defer3.patch b/patches/patches/116_defer3.patch new file mode 100644 index 0000000..7b2cce5 --- /dev/null +++ b/patches/patches/116_defer3.patch @@ -0,0 +1,14 @@ +--- exercises/116_defer3.zig 2026-05-31 22:29:56.189323732 +0200 ++++ answers/116_defer3.zig 2026-05-31 22:30:17.749186667 +0200 +@@ -7,9 +7,9 @@ + 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; ++ defer x = x + 11; ++ defer x = x / 10; + + // It might seem silly in this example, but it's important to know when + // deinitializing containers whose elements need to be deinitialized first. diff --git a/src/elrond.zig b/src/elrond.zig index 35c8857..311026f 100644 --- a/src/elrond.zig +++ b/src/elrond.zig @@ -1223,6 +1223,10 @@ const exercises = [_]Exercise{ .main_file = "115_packed2.zig", .output = "", }, + .{ + .main_file = "116_defer3.zig", + .output = "42", + }, .{ .main_file = "999_the_end.zig", .output = From bd55c4ac5a235778f78b91c37a0bf1516bfbcc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Tue, 2 Jun 2026 06:11:27 +0200 Subject: [PATCH 2/2] fixup! Add another defer exercise --- exercises/028_defer2.zig | 23 +++++++++++++++++++++++ exercises/116_defer3.zig | 18 ------------------ patches/patches/028_defer2.patch | 18 +++++++++++++++--- patches/patches/116_defer3.patch | 14 -------------- src/elrond.zig | 9 ++++----- 5 files changed, 42 insertions(+), 40 deletions(-) delete mode 100644 exercises/116_defer3.zig delete mode 100644 patches/patches/116_defer3.patch diff --git a/exercises/028_defer2.zig b/exercises/028_defer2.zig index 35c1326..b490a89 100644 --- a/exercises/028_defer2.zig +++ b/exercises/028_defer2.zig @@ -10,6 +10,8 @@ pub fn main() void { for (animals) |a| printAnimal(a); 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 @@ -35,3 +37,24 @@ fn printAnimal(animal: u8) void { 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; +} diff --git a/exercises/116_defer3.zig b/exercises/116_defer3.zig deleted file mode 100644 index 6e3cf6d..0000000 --- a/exercises/116_defer3.zig +++ /dev/null @@ -1,18 +0,0 @@ -// -// When there are multiple defers in a single block, they are executed in reverse order. -// -const std = @import("std"); - -pub fn main() void { - 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; - - // It might seem silly in this example, but it's important to know when - // deinitializing containers whose elements need to be deinitialized first. - } - std.debug.print("{d}\n", .{x}); -} diff --git a/patches/patches/028_defer2.patch b/patches/patches/028_defer2.patch index f09b1e5..28eb806 100644 --- a/patches/patches/028_defer2.patch +++ b/patches/patches/028_defer2.patch @@ -1,6 +1,6 @@ ---- exercises/028_defer2.zig 2023-10-03 22:15:22.122241138 +0200 -+++ answers/028_defer2.zig 2023-10-05 20:04:06.966098530 +0200 -@@ -18,7 +18,7 @@ +--- exercises/028_defer2.zig 2026-06-02 06:08:12.713672612 +0200 ++++ answers/028_defer2.zig 2026-06-02 06:08:43.262234023 +0200 +@@ -20,7 +20,7 @@ fn printAnimal(animal: u8) void { std.debug.print("(", .{}); @@ -9,3 +9,15 @@ if (animal == 'g') { 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; diff --git a/patches/patches/116_defer3.patch b/patches/patches/116_defer3.patch deleted file mode 100644 index 7b2cce5..0000000 --- a/patches/patches/116_defer3.patch +++ /dev/null @@ -1,14 +0,0 @@ ---- exercises/116_defer3.zig 2026-05-31 22:29:56.189323732 +0200 -+++ answers/116_defer3.zig 2026-05-31 22:30:17.749186667 +0200 -@@ -7,9 +7,9 @@ - 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; -+ defer x = x + 11; -+ defer x = x / 10; - - // It might seem silly in this example, but it's important to know when - // deinitializing containers whose elements need to be deinitialized first. diff --git a/src/elrond.zig b/src/elrond.zig index 311026f..716f546 100644 --- a/src/elrond.zig +++ b/src/elrond.zig @@ -693,7 +693,10 @@ const exercises = [_]Exercise{ }, .{ .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", @@ -1223,10 +1226,6 @@ const exercises = [_]Exercise{ .main_file = "115_packed2.zig", .output = "", }, - .{ - .main_file = "116_defer3.zig", - .output = "42", - }, .{ .main_file = "999_the_end.zig", .output =