diff options
| author | Nuno Mendes <98030270+nm-remarkable@users.noreply.github.com> | 2024-09-15 20:14:49 +0200 |
|---|---|---|
| committer | Nuno Mendes <98030270+nm-remarkable@users.noreply.github.com> | 2024-09-15 20:14:49 +0200 |
| commit | 335a78f8f5ef8b86922dc241422acb033e0b3b2a (patch) | |
| tree | 24b501b361c4fb7c8437cd7072569e800d22b979 | |
| parent | 102ba8c894ebc39e0d61634ad17ca9751984e715 (diff) | |
108: Add a exercise for a labeled switch
| -rw-r--r-- | build.zig | 6 | ||||
| -rw-r--r-- | exercises/108_labeled_switch.zig | 34 |
2 files changed, 40 insertions, 0 deletions
@@ -1198,6 +1198,12 @@ const exercises = [_]Exercise{ , }, .{ + .main_file = "108_labeled_switch.zig", + .output = + \\The pull request has been merged + , + }, + .{ .main_file = "999_the_end.zig", .output = \\ diff --git a/exercises/108_labeled_switch.zig b/exercises/108_labeled_switch.zig new file mode 100644 index 0000000..623e359 --- /dev/null +++ b/exercises/108_labeled_switch.zig @@ -0,0 +1,34 @@ +// +// A labeled switch in zig allows the usage of continue and break +// just like loops, these allow you to create very concise +// Finite State Automata to represent state transitions +// +// foo: switch (state) { +// 1 => continue :foo 2, +// 2 => continue :foo 3, +// 3 => return, +// 4 => {}, +// ... +// } +// +const std = @import("std"); + +const PullRequestState = enum { + Draft, + InReview, + Approved, + Rejected, + Merged, +}; + +pub fn main() void { + // Something is wrong, it seems your Pull Request can never be merged + // try to fix it! + pr: switch (@as(PullRequestState, PullRequestState.Draft)) { + PullRequestState.Draft => continue :pr PullRequestState.InReview, + PullRequestState.InReview => continue :pr PullRequestState.Rejected, + PullRequestState.Approved => continue :pr PullRequestState.Merged, + PullRequestState.Rejected => std.debug.print("The pull request has been rejected", .{}), + PullRequestState.Merged => std.debug.print("The pull request has been merged", .{}), + } +} |
