authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-18 21:55:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-18 22:01:09-07:00
log96e54e70175881573de2cd1a5e031e391e166f6a
tree456f805b3d5b8a9d9ef209f3b9ea72628b661269
parentab82132749f24702c215545212dd83538a04cdb9

langref: extract code comments into paragraphs

Related #18496

1 files changed, 11 insertions(+), 57 deletions(-)

doc/langref.html.in+11-57
......@@ -1156,10 +1156,8 @@ fn addOne(number: i32) i32 {
11561156 {#link|identifier|Identifiers#}, followed by a {#link|block|Blocks#} containing any valid Zig code that
11571157 is allowed in a {#link|function|Functions#}.
11581158 </p>
1159 <aside>
1160 By convention, non-named tests should only be used to {#link|make other tests run|Nested Container Tests#}.
1161 Non-named tests cannot be {#link|filtered|Skip Tests#}.
1162 </aside>
1159 <p>Non-named test blocks always run during test builds and are exempt from
1160 {#link|Skip Tests#}.</p>
11631161 <p>
11641162 Test declarations are similar to {#link|Functions#}: they have a return type and a block of code. The implicit
11651163 return type of {#syntax#}test{#endsyntax#} is the {#link|Error Union Type#} {#syntax#}anyerror!void{#endsyntax#},
......@@ -5009,12 +5007,12 @@ test "if error union with optional" {
50095007 {#see_also|Optionals|Errors#}
50105008 {#header_close#}
50115009 {#header_open|defer#}
5010 <p>Executes an expression unconditionally at scope exit.</p>
50125011 {#code_begin|test|test_defer#}
50135012const std = @import("std");
50145013const expect = std.testing.expect;
50155014const print = std.debug.print;
50165015
5017// defer will execute an expression at the end of the current scope.
50185016fn deferExample() !usize {
50195017 var a: usize = 1;
50205018
......@@ -5031,10 +5029,14 @@ fn deferExample() !usize {
50315029test "defer basics" {
50325030 try expect((try deferExample()) == 5);
50335031}
5032 {#code_end#}
5033 <p>Defer expressions are evaluated in reverse order.</p>
5034 {#code_begin|test|defer_unwind#}
5035const std = @import("std");
5036const expect = std.testing.expect;
5037const print = std.debug.print;
50345038
5035// If multiple defer statements are specified, they will be executed in
5036// the reverse order they were run.
5037fn deferUnwindExample() void {
5039test "defer unwinding" {
50385040 print("\n", .{});
50395041
50405042 defer {
......@@ -5050,63 +5052,15 @@ fn deferUnwindExample() void {
50505052 }
50515053 }
50525054}
5053
5054test "defer unwinding" {
5055 deferUnwindExample();
5056}
50575055 {#code_end#}
5056 <p>Inside a defer expression the return statement is not allowed.</p>
50585057 {#code_begin|test_err|test_invalid_defer|cannot return from defer expression#}
5059// Inside a defer expression the return statement is not allowed.
50605058fn deferInvalidExample() !void {
50615059 defer {
50625060 return error.DeferError;
50635061 }
50645062
50655063 return error.DeferError;
5066}
5067 {#code_end#}
5068 {#code_begin|test|test_errdefer#}
5069const std = @import("std");
5070const print = std.debug.print;
5071
5072// The errdefer keyword is similar to defer, but will only execute if the
5073// scope returns with an error.
5074//
5075// This is especially useful in allowing a function to clean up properly
5076// on error, and replaces goto error handling tactics as seen in c.
5077fn deferErrorExample(is_error: bool) !void {
5078 print("\nstart of function\n", .{});
5079
5080 // This will always be executed on exit
5081 defer {
5082 print("end of function\n", .{});
5083 }
5084
5085 errdefer {
5086 print("encountered an error!\n", .{});
5087 }
5088
5089 if (is_error) {
5090 return error.DeferError;
5091 }
5092}
5093
5094// The errdefer keyword also supports an alternative syntax to capture the
5095// generated error.
5096//
5097// This is useful for printing an additional error message during clean up.
5098fn deferErrorCaptureExample() !void {
5099 errdefer |err| {
5100 std.debug.print("the error is {s}\n", .{@errorName(err)});
5101 }
5102
5103 return error.DeferError;
5104}
5105
5106test "errdefer unwinding" {
5107 deferErrorExample(false) catch {};
5108 deferErrorExample(true) catch {};
5109 deferErrorCaptureExample() catch {};
51105064}
51115065 {#code_end#}
51125066 {#see_also|Errors#}