Merge pull request 'I/O improvements' (#355) from io_improvements into main

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/355
This commit is contained in:
Chris Boesch 2026-01-09 23:07:31 +01:00
commit 07583db582
8 changed files with 26 additions and 28 deletions

View File

@ -5,9 +5,6 @@
// //
const std = @import("std"); 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 // Take note that this main() definition now returns "!void" rather
// than just "void". Since there's no specific error type, this means // 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 // 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: // You can find more information at:
// https://ziglang.org/documentation/master/#Inferred-Error-Sets // 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... // We get a Writer for Standard Out...
var stdout_writer = std.Io.File.stdout().writer(io, &.{}); var stdout_writer = std.Io.File.stdout().writer(io, &.{});
// ...and extract its interface so we can print() to it. // ...and extract its interface so we can print() to it.

View File

@ -6,11 +6,11 @@
// my_num=42 // my_num=42
// //
const std = @import("std"); const std = @import("std");
const io = std.Options.debug_io;
const NumError = error{IllegalNumber}; 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, &.{}); var stdout_writer = std.Io.File.stdout().writer(io, &.{});
const stdout = &stdout_writer.interface; const stdout = &stdout_writer.interface;

View File

@ -24,9 +24,11 @@
// performance. We'll learn about buffered I/O in a later exercise. // performance. We'll learn about buffered I/O in a later exercise.
// //
const std = @import("std"); 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 // first we get the current working directory
const cwd: std.Io.Dir = std.Io.Dir.cwd(); const cwd: std.Io.Dir = std.Io.Dir.cwd();

View File

@ -21,9 +21,10 @@
// performance. We'll learn about buffered I/O in a later exercise. // performance. We'll learn about buffered I/O in a later exercise.
const std = @import("std"); 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 // Get the current working directory
const cwd = std.Io.Dir.cwd(); const cwd = std.Io.Dir.cwd();

View File

@ -1,5 +1,5 @@
--- exercises/026_hello2.zig 2025-12-30 13:39:29.478620712 +0100 --- exercises/026_hello2.zig 2026-01-09 22:51:45.803358789 +0100
+++ answers/026_hello2.zig 2025-12-30 13:39:18.948412943 +0100 +++ answers/026_hello2.zig 2026-01-09 22:50:46.016166527 +0100
@@ -28,5 +28,5 @@ @@ -28,5 +28,5 @@
// to be able to pass it up as a return value of main(). // to be able to pass it up as a return value of main().
// //

View File

@ -1,11 +1,6 @@
--- exercises/034_quiz4.zig 2025-12-28 14:43:41.087943476 +0100 --- exercises/034_quiz4.zig 2026-01-09 22:45:53.115325559 +0100
+++ answers/034_quiz4.zig 2025-12-28 14:42:23.878472164 +0100 +++ answers/034_quiz4.zig 2026-01-09 22:45:15.658578603 +0100
@@ -10,11 +10,11 @@ @@ -14,7 +14,7 @@
const NumError = error{IllegalNumber};
-pub fn main() void {
+pub fn main() !void {
var stdout_writer = std.Io.File.stdout().writer(io, &.{}); var stdout_writer = std.Io.File.stdout().writer(io, &.{});
const stdout = &stdout_writer.interface; const stdout = &stdout_writer.interface;

View File

@ -1,6 +1,6 @@
--- exercises/106_files.zig 2025-12-28 20:38:53.005504479 +0100 --- exercises/106_files.zig 2026-01-09 22:41:19.373872684 +0100
+++ answers/106_files.zig 2025-12-28 20:37:42.742154100 +0100 +++ answers/106_files.zig 2026-01-09 22:41:44.518372910 +0100
@@ -39,7 +39,7 @@ @@ -41,7 +41,7 @@
// by doing nothing // by doing nothing
// //
// we want to catch error.PathAlreadyExists and do 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 // if there's any other unexpected error we just propagate it through
else => return e, else => return e,
}; };
@@ -59,7 +59,7 @@ @@ -61,7 +61,7 @@
// but here we are not yet done writing to the file // but here we are not yet done writing to the file
// if only there were a keyword in Zig that // if only there were a keyword in Zig that
// allowed you to "defer" code execution to the end of the scope... // allowed you to "defer" code execution to the end of the scope...

View File

@ -1,6 +1,6 @@
--- exercises/107_files2.zig 2025-12-28 21:17:29.658147954 +0100 --- exercises/107_files2.zig 2026-01-09 22:43:15.211177186 +0100
+++ answers/107_files2.zig 2025-12-28 21:17:10.585787203 +0100 +++ answers/107_files2.zig 2026-01-09 22:42:48.943654602 +0100
@@ -38,7 +38,7 @@ @@ -39,7 +39,7 @@
// initialize an array of u8 with all letter 'A' // initialize an array of u8 with all letter 'A'
// we need to pick the size of the array, 64 seems like a good number // we need to pick the size of the array, 64 seems like a good number
// fix the initialization below // fix the initialization below
@ -9,7 +9,7 @@
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content}); 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? // can you go here to find a way to read the content?
// https://ziglang.org/documentation/master/std/#std.Io.Reader // https://ziglang.org/documentation/master/std/#std.Io.Reader
// hint: look for a method that reads into a slice // hint: look for a method that reads into a slice