108_labeled_switch.zig 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // 4 => {},
  19. // }
  20. // break;
  21. // }
  22. // std.debug.print("This statement cannot be reached");
  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 syntatic 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. // 4 => {},
  38. // }
  39. // std.debug.print("This statement cannot be reached");
  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. //
  50. // Since step 4 or a break stament do not exist in this switch, the debug statement is
  51. // never executed
  52. //
  53. const std = @import("std");
  54. const PullRequestState = enum(u8) {
  55. Draft,
  56. InReview,
  57. Approved,
  58. Rejected,
  59. Merged,
  60. };
  61. pub fn main() void {
  62. // Oh no, your pull request keeps being rejected,
  63. // how would you fix it?
  64. pr: switch (PullRequestState.Draft) {
  65. PullRequestState.Draft => continue :pr PullRequestState.InReview,
  66. PullRequestState.InReview => continue :pr PullRequestState.Rejected,
  67. PullRequestState.Approved => continue :pr PullRequestState.Merged,
  68. PullRequestState.Rejected => {
  69. std.debug.print("The pull request has been rejected.\n", .{});
  70. return;
  71. },
  72. PullRequestState.Merged => break, // Would you know where to break to?
  73. }
  74. std.debug.print("The pull request has been merged.\n", .{});
  75. }