081_anonymous_structs2.zig 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // An anonymous struct value LITERAL (not to be confused with a
  3. // struct TYPE) uses '.{}' syntax:
  4. //
  5. // .{
  6. // .center_x = 15,
  7. // .center_y = 12,
  8. // .radius = 6,
  9. // }
  10. //
  11. // These literals are always evaluated entirely at compile-time.
  12. // The example above could be coerced into the i32 variant of the
  13. // "circle struct" from the last exercise.
  14. //
  15. // Or you can let them remain entirely anonymous as in this
  16. // example:
  17. //
  18. // fn bar(foo: anytype) void {
  19. // print("a:{} b:{}\n", .{foo.a, foo.b});
  20. // }
  21. //
  22. // bar(.{
  23. // .a = true,
  24. // .b = false,
  25. // });
  26. //
  27. // The example above prints "a:true b:false".
  28. //
  29. const print = @import("std").debug.print;
  30. pub fn main() void {
  31. printCircle(.{
  32. .center_x = @as(u32, 205),
  33. .center_y = @as(u32, 187),
  34. .radius = @as(u32, 12),
  35. });
  36. }
  37. // Please complete this function which prints an anonymous struct
  38. // representing a circle.
  39. fn printCircle(???) void {
  40. print("x:{} y:{} radius:{}\n", .{
  41. circle.center_x,
  42. circle.centaur_y,
  43. circle.radius,
  44. });
  45. }