mirror of
https://codeberg.org/ziglings/exercises.git
synced 2024-12-25 17:20:26 +00:00
108: Add a exercise for a labeled switch
This commit is contained in:
parent
102ba8c894
commit
335a78f8f5
2 changed files with 40 additions and 0 deletions
|
@ -1197,6 +1197,12 @@ const exercises = [_]Exercise{
|
||||||
\\Successfully Read 18 bytes: It's zigling time!
|
\\Successfully Read 18 bytes: It's zigling time!
|
||||||
,
|
,
|
||||||
},
|
},
|
||||||
|
.{
|
||||||
|
.main_file = "108_labeled_switch.zig",
|
||||||
|
.output =
|
||||||
|
\\The pull request has been merged
|
||||||
|
,
|
||||||
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "999_the_end.zig",
|
.main_file = "999_the_end.zig",
|
||||||
.output =
|
.output =
|
||||||
|
|
34
exercises/108_labeled_switch.zig
Normal file
34
exercises/108_labeled_switch.zig
Normal file
|
@ -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", .{}),
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue