086_async3.zig 643 B

1234567891011121314151617181920212223242526272829
  1. //
  2. // Because they can suspend and resume, async Zig functions are
  3. // an example of a more general programming concept called
  4. // "coroutines". One of the neat things about Zig async functions
  5. // is that they retain their state as they are suspended and
  6. // resumed.
  7. //
  8. // See if you can make this program print "5 4 3 2 1".
  9. //
  10. const print = @import("std").debug.print;
  11. pub fn main() void {
  12. const n = 5;
  13. var foo_frame = async foo(n);
  14. ???
  15. print("\n", .{});
  16. }
  17. fn foo(countdown: u32) void {
  18. var current = countdown;
  19. while (current > 0) {
  20. print("{} ", .{current});
  21. current -= 1;
  22. suspend;
  23. }
  24. }