From 1e552a1dd6dac663a9329e5c71c5c7c245f2ed1e Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Fri, 9 Jan 2026 22:56:23 +0100 Subject: [PATCH] I/O improvements --- exercises/026_hello2.zig | 8 ++++---- exercises/034_quiz4.zig | 4 ++-- exercises/106_files.zig | 6 ++++-- exercises/107_files2.zig | 5 +++-- patches/patches/026_hello2.patch | 4 ++-- patches/patches/034_quiz4.patch | 11 +++-------- patches/patches/106_files.patch | 8 ++++---- patches/patches/107_files2.patch | 8 ++++---- 8 files changed, 26 insertions(+), 28 deletions(-) diff --git a/exercises/026_hello2.zig b/exercises/026_hello2.zig index 9cd8bbc..f110d87 100644 --- a/exercises/026_hello2.zig +++ b/exercises/026_hello2.zig @@ -5,9 +5,6 @@ // const std = @import("std"); -// Instance for input/output operations; we will learn more about this later. -const io = std.Options.debug_io; - // Take note that this main() definition now returns "!void" rather // than just "void". Since there's no specific error type, this means // that Zig will infer the error type. This is appropriate in the case @@ -17,7 +14,10 @@ const io = std.Options.debug_io; // You can find more information at: // https://ziglang.org/documentation/master/#Inferred-Error-Sets // -pub fn main() !void { +pub fn main(init: std.process.Init) !void { + // Instance for input/output operations; we will learn more about this later. + const io = init.io; + // We get a Writer for Standard Out... var stdout_writer = std.Io.File.stdout().writer(io, &.{}); // ...and extract its interface so we can print() to it. diff --git a/exercises/034_quiz4.zig b/exercises/034_quiz4.zig index 28f2291..b33a6f5 100644 --- a/exercises/034_quiz4.zig +++ b/exercises/034_quiz4.zig @@ -6,11 +6,11 @@ // my_num=42 // const std = @import("std"); -const io = std.Options.debug_io; const NumError = error{IllegalNumber}; -pub fn main() void { +pub fn main(init: std.process.Init) !void { + const io = init.io; var stdout_writer = std.Io.File.stdout().writer(io, &.{}); const stdout = &stdout_writer.interface; diff --git a/exercises/106_files.zig b/exercises/106_files.zig index b2ebb17..bf4de9b 100644 --- a/exercises/106_files.zig +++ b/exercises/106_files.zig @@ -24,9 +24,11 @@ // performance. We'll learn about buffered I/O in a later exercise. // const std = @import("std"); -const io = std.Options.debug_io; -pub fn main() !void { +pub fn main(init: std.process.Init) !void { + // default I/O implementation + const io = init.io; + // first we get the current working directory const cwd: std.Io.Dir = std.Io.Dir.cwd(); diff --git a/exercises/107_files2.zig b/exercises/107_files2.zig index a11c46a..c363086 100644 --- a/exercises/107_files2.zig +++ b/exercises/107_files2.zig @@ -21,9 +21,10 @@ // performance. We'll learn about buffered I/O in a later exercise. const std = @import("std"); -const io = std.Options.debug_io; -pub fn main() !void { +pub fn main(init: std.process.Init) !void { + const io = init.io; + // Get the current working directory const cwd = std.Io.Dir.cwd(); diff --git a/patches/patches/026_hello2.patch b/patches/patches/026_hello2.patch index 1616c08..85f02a7 100644 --- a/patches/patches/026_hello2.patch +++ b/patches/patches/026_hello2.patch @@ -1,5 +1,5 @@ ---- exercises/026_hello2.zig 2025-12-30 13:39:29.478620712 +0100 -+++ answers/026_hello2.zig 2025-12-30 13:39:18.948412943 +0100 +--- exercises/026_hello2.zig 2026-01-09 22:51:45.803358789 +0100 ++++ answers/026_hello2.zig 2026-01-09 22:50:46.016166527 +0100 @@ -28,5 +28,5 @@ // to be able to pass it up as a return value of main(). // diff --git a/patches/patches/034_quiz4.patch b/patches/patches/034_quiz4.patch index 7cf102c..8fad042 100644 --- a/patches/patches/034_quiz4.patch +++ b/patches/patches/034_quiz4.patch @@ -1,11 +1,6 @@ ---- exercises/034_quiz4.zig 2025-12-28 14:43:41.087943476 +0100 -+++ answers/034_quiz4.zig 2025-12-28 14:42:23.878472164 +0100 -@@ -10,11 +10,11 @@ - - const NumError = error{IllegalNumber}; - --pub fn main() void { -+pub fn main() !void { +--- exercises/034_quiz4.zig 2026-01-09 22:45:53.115325559 +0100 ++++ answers/034_quiz4.zig 2026-01-09 22:45:15.658578603 +0100 +@@ -14,7 +14,7 @@ var stdout_writer = std.Io.File.stdout().writer(io, &.{}); const stdout = &stdout_writer.interface; diff --git a/patches/patches/106_files.patch b/patches/patches/106_files.patch index 8bff4d6..27bcb56 100644 --- a/patches/patches/106_files.patch +++ b/patches/patches/106_files.patch @@ -1,6 +1,6 @@ ---- exercises/106_files.zig 2025-12-28 20:38:53.005504479 +0100 -+++ answers/106_files.zig 2025-12-28 20:37:42.742154100 +0100 -@@ -39,7 +39,7 @@ +--- exercises/106_files.zig 2026-01-09 22:41:19.373872684 +0100 ++++ answers/106_files.zig 2026-01-09 22:41:44.518372910 +0100 +@@ -41,7 +41,7 @@ // by doing nothing // // we want to catch error.PathAlreadyExists and do nothing @@ -9,7 +9,7 @@ // if there's any other unexpected error we just propagate it through else => return e, }; -@@ -59,7 +59,7 @@ +@@ -61,7 +61,7 @@ // but here we are not yet done writing to the file // if only there were a keyword in Zig that // allowed you to "defer" code execution to the end of the scope... diff --git a/patches/patches/107_files2.patch b/patches/patches/107_files2.patch index 597c260..6820742 100644 --- a/patches/patches/107_files2.patch +++ b/patches/patches/107_files2.patch @@ -1,6 +1,6 @@ ---- exercises/107_files2.zig 2025-12-28 21:17:29.658147954 +0100 -+++ answers/107_files2.zig 2025-12-28 21:17:10.585787203 +0100 -@@ -38,7 +38,7 @@ +--- exercises/107_files2.zig 2026-01-09 22:43:15.211177186 +0100 ++++ answers/107_files2.zig 2026-01-09 22:42:48.943654602 +0100 +@@ -39,7 +39,7 @@ // initialize an array of u8 with all letter 'A' // we need to pick the size of the array, 64 seems like a good number // fix the initialization below @@ -9,7 +9,7 @@ // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` std.debug.print("{s}\n", .{content}); -@@ -49,12 +49,12 @@ +@@ -50,12 +50,12 @@ // can you go here to find a way to read the content? // https://ziglang.org/documentation/master/std/#std.Io.Reader // hint: look for a method that reads into a slice