Add another defer exercise

This commit is contained in:
Luka Markušić
2026-05-31 22:26:17 +02:00
parent ac9f96459c
commit e61bedaa25
3 changed files with 36 additions and 0 deletions

18
exercises/116_defer3.zig Normal file
View File

@@ -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});
}