summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'exercises')
-rw-r--r--exercises/028_defer2.zig23
-rw-r--r--exercises/110_files2.zig6
2 files changed, 26 insertions, 3 deletions
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/110_files2.zig b/exercises/110_files2.zig
index 9dfaf05..c4cce08 100644
--- a/exercises/110_files2.zig
+++ b/exercises/110_files2.zig
@@ -36,10 +36,10 @@ pub fn main(init: std.process.Init) !void {
const file = try output_dir.openFile(io, "zigling.txt", .{});
defer file.close(io);
- // initialize an array of u8 with all letter 'A'
+ // initialize an array of u8 entirely with the letter 'A'
// we need to pick the size of the array, 64 seems like a good number
- // fix the initialization below
- var content = ['A']*64;
+ // do you remember the array repetition function?
+ var content: ??? = ???('A');
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content});