test_pass_by_reference_or_value.zig (537B) - Raw
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 expect = @import("std").testing.expect; 15 16 test "pass struct to function" { 17 try expect(foo(Point{ .x = 1, .y = 2 }) == 3); 18 } 19 20 // test