108_labeled_switch.zig 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // You've heard of while loops in exercises 011,012,013 and 014
  3. // You've also heard of switch expressions in exercises 030 and 31.
  4. // You've also seen how labels can be used in exercise 063.
  5. //
  6. // By combining while loops and switch statements with continue and break statements
  7. // one can create very concise State Machines.
  8. //
  9. // One such example would be:
  10. //
  11. // pub fn main() void {
  12. // var op: u8 = 1;
  13. // while (true) {
  14. // switch (op) {
  15. // 1 => { op = 2; continue; },
  16. // 2 => { op = 3; continue; },
  17. // 3 => return,
  18. // else => {},
  19. // }
  20. // break;
  21. // }
  22. // std.debug.print("This statement cannot be reached\n", .{});
  23. // }
  24. //
  25. // By combining all we've learned so far, we can now proceed with a labeled switch
  26. //
  27. // A labeled switch is some extra syntactic sugar, which comes with all sorts of
  28. // candy (performance benefits). Don't believe me? Directly to source https://github.com/ziglang/zig/pull/21367
  29. //
  30. // Here is the previous excerpt implemented as a labeled switch instead:
  31. //
  32. // pub fn main() void {
  33. // foo: switch (@as(u8, 1)) {
  34. // 1 => continue :foo 2,
  35. // 2 => continue :foo 3,
  36. // 3 => return,
  37. // else => {},
  38. // }
  39. // std.debug.print("This statement cannot be reached\n", .{});
  40. // }
  41. //
  42. // The flow of execution on this second case is:
  43. // 1. The switch starts with value '1';
  44. // 2. The switch evaluates to case '1' which in turn uses the continue statement
  45. // to re-evaluate the labeled switch again, now providing the value '2';
  46. // 3. In the case '2' we repeat the same pattern as case '1'
  47. // but instead the value to be evaluated is now '3';
  48. // 4. Finally we get to case '3', where we return from the function as a whole,
  49. // so the debug statement is never executed.
  50. // 5. In this example, since the input does not have clear, exhaustive patterns and
  51. // can essentially be any 'u8' integer, we need to handle all cases not explicitly
  52. // covered by using the 'else => {}' branch as the default case.
  53. //
  54. //
  55. const std = @import("std");
  56. const PullRequestState = enum(u8) {
  57. Draft,
  58. InReview,
  59. Approved,
  60. Rejected,
  61. Merged,
  62. };
  63. pub fn main() void {
  64. // Oh no, your pull request keeps being rejected,
  65. // how would you fix it?
  66. pr: switch (PullRequestState.Draft) {
  67. PullRequestState.Draft => continue :pr PullRequestState.InReview,
  68. PullRequestState.InReview => continue :pr PullRequestState.Rejected,
  69. PullRequestState.Approved => continue :pr PullRequestState.Merged,
  70. PullRequestState.Rejected => {
  71. std.debug.print("The pull request has been rejected.\n", .{});
  72. return;
  73. },
  74. PullRequestState.Merged => break, // Would you know where to break to?
  75. }
  76. std.debug.print("The pull request has been merged.\n", .{});
  77. }