update labeled switch to also have a break statement

This commit is contained in:
Nuno Mendes 2024-09-16 18:38:43 +02:00
parent 798bb40396
commit db569a1478
2 changed files with 21 additions and 10 deletions

View file

@ -6,14 +6,14 @@
// foo: switch (state) { // foo: switch (state) {
// 1 => continue :foo 2, // 1 => continue :foo 2,
// 2 => continue :foo 3, // 2 => continue :foo 3,
// 3 => return, // 3 => break :foo,
// 4 => {}, // 4 => {},
// ... // ...
// } // }
// //
const std = @import("std"); const std = @import("std");
const PullRequestState = enum { const PullRequestState = enum(u8) {
Draft, Draft,
InReview, InReview,
Approved, Approved,
@ -22,13 +22,17 @@ const PullRequestState = enum {
}; };
pub fn main() void { pub fn main() void {
// Something is wrong, it seems your Pull Request can never be merged // Oh no, your pull request keeps being rejected,
// try to fix it! // how would you fix it?
pr: switch (@as(PullRequestState, PullRequestState.Draft)) { pr: switch (@as(PullRequestState, PullRequestState.Draft)) {
PullRequestState.Draft => continue :pr PullRequestState.InReview, PullRequestState.Draft => continue :pr PullRequestState.InReview,
PullRequestState.InReview => continue :pr PullRequestState.Rejected, PullRequestState.InReview => continue :pr PullRequestState.Rejected,
PullRequestState.Approved => continue :pr PullRequestState.Merged, PullRequestState.Approved => continue :pr PullRequestState.Merged,
PullRequestState.Rejected => std.debug.print("The pull request has been rejected", .{}), PullRequestState.Rejected => {
PullRequestState.Merged => std.debug.print("The pull request has been merged", .{}), std.debug.print("The pull request has been rejected", .{});
return;
},
PullRequestState.Merged => break, // Would you know where to break to?
} }
std.debug.print("The pull request has been merged", .{});
} }

View file

@ -1,11 +1,18 @@
--- a/exercises/108_labeled_switch.zig --- a/exercises/108_labeled_switch.zig
+++ b/exercises/108_labeled_switch.zig +++ b/exercises/108_labeled_switch.zig
@@ -26,7 +26,7 @@ pub fn main() void { @@ -26,13 +26,13 @@ pub fn main() void {
// try to fix it! // how would you fix it?
pr: switch (@as(PullRequestState, PullRequestState.Draft)) { pr: switch (@as(PullRequestState, PullRequestState.Draft)) {
PullRequestState.Draft => continue :pr PullRequestState.InReview, PullRequestState.Draft => continue :pr PullRequestState.InReview,
- PullRequestState.InReview => continue :pr PullRequestState.Rejected, - PullRequestState.InReview => continue :pr PullRequestState.Rejected,
+ PullRequestState.InReview => continue :pr PullRequestState.Approved, + PullRequestState.InReview => continue :pr PullRequestState.Approved,
PullRequestState.Approved => continue :pr PullRequestState.Merged, PullRequestState.Approved => continue :pr PullRequestState.Merged,
PullRequestState.Rejected => std.debug.print("The pull request has been rejected", .{}), PullRequestState.Rejected => {
PullRequestState.Merged => std.debug.print("The pull request has been merged", .{}), std.debug.print("The pull request has been rejected", .{});
return;
},
- PullRequestState.Merged => break, // Would you know where to break to?
+ PullRequestState.Merged => break :pr, // Would you know where to break to?
}
std.debug.print("The pull request has been merged", .{});
}