langref: add example for errdefer |err| sytnax

This commit is contained in:
Alex Kladov
2024-06-28 13:32:56 +01:00
committed by Andrew Kelley
parent aa73bb6bc9
commit 8267929742
2 changed files with 23 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
const std = @import("std");
fn captureError(captured: *?anyerror) !void {
errdefer |err| {
captured.* = err;
}
return error.GeneralFailure;
}
test "errdefer capture" {
var captured: ?anyerror = null;
if (captureError(&captured)) unreachable else |err| {
try std.testing.expectEqual(error.GeneralFailure, captured.?);
try std.testing.expectEqual(error.GeneralFailure, err);
}
}
// test