| 1 | const Point = struct { |
| 2 | x: i32, |
| 3 | y: i32, |
| 4 | }; |
| 5 | |
| 6 | fn foo(point: Point) i32 { |
| 7 | // Here, `point` could be a reference, or a copy. The function body |
| 8 | // can ignore the difference and treat it as a value. Be very careful |
| 9 | // taking the address of the parameter - it should be treated as if |
| 10 | // the address will become invalid when the function returns. |
| 11 | return point.x + point.y; |
| 12 | } |
| 13 | |
| 14 | const expectEqual = @import("std").testing.expectEqual; |
| 15 | |
| 16 | test "pass struct to function" { |
| 17 | try expectEqual(3, foo(Point{ .x = 1, .y = 2 })); |
| 18 | } |
| 19 | |
| 20 | // test |