zig/doc/langref/test_pass_by_reference_or_value.zig

20 lines
550 B
Zig

const Point = struct {
x: i32,
y: i32,
};
fn foo(point: Point) i32 {
// Here, `point` could be a reference, or a copy. The function body
// can ignore the difference and treat it as a value. Be very careful
// taking the address of the parameter - it should be treated as if
// the address will become invalid when the function returns.
return point.x + point.y;
}
const expectEqual = @import("std").testing.expectEqual;
test "pass struct to function" {
try expectEqual(3, foo(Point{ .x = 1, .y = 2 }));
}
// test