This commit is contained in:
2026-06-04 15:03:31 +02:00
parent 77e39463d4
commit b8ba0dbb54
6 changed files with 10 additions and 6 deletions
+1 -1
View File
@@ -28,5 +28,5 @@ pub fn main(init: std.process.Init) !void {
// 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().
// //
// We just learned of a single statement which can accomplish this. // We just learned of a single statement which can accomplish this.
stdout.print("Hello world!\n", .{}); try stdout.print("Hello world!\n", .{});
} }
+1 -1
View File
@@ -20,6 +20,6 @@ const std = @import("std");
pub fn main() void { pub fn main() void {
// Without changing anything else, please add a 'defer' statement // Without changing anything else, please add a 'defer' statement
// to this code so that our program prints "One Two\n": // to this code so that our program prints "One Two\n":
std.debug.print("Two\n", .{}); defer std.debug.print("Two\n", .{});
std.debug.print("One ", .{}); std.debug.print("One ", .{});
} }
+3 -3
View File
@@ -20,7 +20,7 @@ pub fn main() void {
fn printAnimal(animal: u8) void { fn printAnimal(animal: u8) void {
std.debug.print("(", .{}); std.debug.print("(", .{});
std.debug.print(") ", .{}); // <---- how?! defer std.debug.print(") ", .{}); // <---- how?!
if (animal == 'g') { if (animal == 'g') {
std.debug.print("Goat", .{}); std.debug.print("Goat", .{});
@@ -51,9 +51,9 @@ fn calculateTheUltimateQuestionOfLife() u32 {
// Try reordering the statements to get the answer 42 // Try reordering the statements to get the answer 42
{ {
defer x = x / 10;
defer x = x + 11;
defer x = x * 2; defer x = x * 2;
defer x = x + 11;
defer x = x / 10;
} }
return x; return x;
+1 -1
View File
@@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 {
// Please make the "failed" message print ONLY if the makeNumber() // Please make the "failed" message print ONLY if the makeNumber()
// function exits with an error: // function exits with an error:
std.debug.print("failed!\n", .{}); errdefer std.debug.print("failed!\n", .{});
var num = try getNumber(); // <-- This could fail! var num = try getNumber(); // <-- This could fail!
+3
View File
@@ -46,6 +46,9 @@ pub fn main() void {
// match for every possible value). Please add an "else" // match for every possible value). Please add an "else"
// to this switch to print a question mark "?" when c is // to this switch to print a question mark "?" when c is
// not one of the existing matches. // not one of the existing matches.
else => {
std.debug.print("?", .{});
},
} }
} }
+1
View File
@@ -29,6 +29,7 @@ pub fn main() void {
// ... // ...
25 => 'Y', 25 => 'Y',
26 => 'Z', 26 => 'Z',
else => '!',
// As in the last exercise, please add the 'else' clause // As in the last exercise, please add the 'else' clause
// and this time, have it return an exclamation mark '!'. // and this time, have it return an exclamation mark '!'.
}; };