@@ -4640,13 +4640,23 @@ test "while null capture" {
4640
4640
}
4641
4641
try expect(sum1 == 3);
4642
4642
4643
+ // null capture with an else block
4643
4644
var sum2: u32 = 0;
4644
4645
numbers_left = 3;
4645
4646
while (eventuallyNullSequence()) |value| {
4646
4647
sum2 += value;
4647
4648
} else {
4648
4649
try expect(sum2 == 3);
4649
4650
}
4651
+
4652
+ // null capture with a continue expression
4653
+ var i: u32 = 0;
4654
+ var sum3: u32 = 0;
4655
+ numbers_left = 3;
4656
+ while (eventuallyNullSequence()) |value| : (i += 1) {
4657
+ sum3 += value;
4658
+ }
4659
+ try expect(i == 3);
4650
4660
}
4651
4661
4652
4662
var numbers_left: u32 = undefined;
@@ -4927,87 +4937,92 @@ test "if boolean" {
4927
4937
}
4928
4938
}
4929
4939
4930
- test "if optional" {
4931
- // If expressions test for null.
4940
+ test "if error union" {
4941
+ // If expressions test for errors.
4942
+ // Note the |err| capture on the else.
4932
4943
4933
- const a: ? u32 = 0;
4944
+ const a: anyerror! u32 = 0;
4934
4945
if (a) |value| {
4935
4946
try expect(value == 0);
4936
- } else {
4947
+ } else |err| {
4948
+ _ = err;
4937
4949
unreachable;
4938
4950
}
4939
4951
4940
- const b: ?u32 = null;
4941
- if (b) |_| {
4952
+ const b: anyerror!u32 = error.BadValue;
4953
+ if (b) |value| {
4954
+ _ = value;
4942
4955
unreachable;
4943
- } else {
4944
- try expect(true );
4956
+ } else |err| {
4957
+ try expect(err == error.BadValue );
4945
4958
}
4946
4959
4947
- // The else is not required.
4960
+ // The else and |err| capture is strictly required.
4948
4961
if (a) |value| {
4949
4962
try expect(value == 0);
4950
- }
4963
+ } else |_| {}
4951
4964
4952
- // To test against null only , use the binary equality operator .
4953
- if (b == null) {
4954
- try expect(true );
4965
+ // To check only the error value , use an empty block expression .
4966
+ if (b) |_| {} else |err| {
4967
+ try expect(err == error.BadValue );
4955
4968
}
4956
4969
4957
4970
// Access the value by reference using a pointer capture.
4958
- var c: ? u32 = 3;
4971
+ var c: anyerror! u32 = 3;
4959
4972
if (c) |*value| {
4960
- value.* = 2;
4973
+ value.* = 9;
4974
+ } else |_| {
4975
+ unreachable;
4961
4976
}
4962
4977
4963
4978
if (c) |value| {
4964
- try expect(value == 2 );
4965
- } else {
4979
+ try expect(value == 9 );
4980
+ } else |_| {
4966
4981
unreachable;
4967
4982
}
4968
4983
}
4984
+ {#code_end#}
4985
+ {#header_open|if with Optionals#}
4969
4986
4970
- test "if error union" {
4971
- // If expressions test for errors.
4972
- // Note the |err| capture on the else.
4987
+ {#code_begin|test|test_if_optionals#}
4988
+ const expect = @import("std").testing.expect;
4973
4989
4974
- const a: anyerror!u32 = 0;
4990
+ test "if optional" {
4991
+ // If expressions test for null.
4992
+
4993
+ const a: ?u32 = 0;
4975
4994
if (a) |value| {
4976
4995
try expect(value == 0);
4977
- } else |err| {
4978
- _ = err;
4996
+ } else {
4979
4997
unreachable;
4980
4998
}
4981
4999
4982
- const b: anyerror!u32 = error.BadValue;
4983
- if (b) |value| {
4984
- _ = value;
5000
+ const b: ?u32 = null;
5001
+ if (b) |_| {
4985
5002
unreachable;
4986
- } else |err| {
4987
- try expect(err == error.BadValue );
5003
+ } else {
5004
+ try expect(true );
4988
5005
}
4989
5006
4990
- // The else and |err| capture is strictly required.
5007
+ // The else is not required.
4991
5008
if (a) |value| {
4992
5009
try expect(value == 0);
4993
- } else |_| {}
5010
+ }
4994
5011
4995
- // To check only the error value , use an empty block expression .
4996
- if (b) |_| {} else |err| {
4997
- try expect(err == error.BadValue );
5012
+ // To test against null only , use the binary equality operator .
5013
+ if (b == null) {
5014
+ try expect(true );
4998
5015
}
4999
5016
5000
5017
// Access the value by reference using a pointer capture.
5001
- var c: anyerror! u32 = 3;
5018
+ var c: ? u32 = 3;
5002
5019
if (c) |*value| {
5003
- value.* = 9;
5004
- } else |_| {
5005
- unreachable;
5020
+ value.* = 2;
5006
5021
}
5007
5022
5008
5023
if (c) |value| {
5009
- try expect(value == 9 );
5010
- } else |_| {
5024
+ try expect(value == 2 );
5025
+ } else {
5011
5026
unreachable;
5012
5027
}
5013
5028
}
@@ -5056,6 +5071,7 @@ test "if error union with optional" {
5056
5071
}
5057
5072
}
5058
5073
{#code_end#}
5074
+ {#header_close#}
5059
5075
{#see_also|Optionals|Errors#}
5060
5076
{#header_close#}
5061
5077
{#header_open|defer#}
@@ -6379,6 +6395,8 @@ test "optional pointers" {
6379
6395
}
6380
6396
{#code_end#}
6381
6397
{#header_close#}
6398
+
6399
+ {#see_also|while with Optionals|if with Optionals#}
6382
6400
{#header_close#}
6383
6401
{#header_open|Casting#}
6384
6402
<p>
0 commit comments