From 5fcc922ff2e0cfc52122e0a00270463e4d58ff00 Mon Sep 17 00:00:00 2001 From: Devin Bayer Date: Sat, 1 May 2021 01:05:45 +0200 Subject: [PATCH] add doc in `Anonymous Struct Literal` section for special @"0" syntax. (#8630) --- doc/langref.html.in | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 05bdfbd043954f748d9599ce477708eebbb87893..744d33f85c419aeae36d003743bbad6acbe4a0bc 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2861,6 +2861,37 @@ fn dump(args: anytype) void { expect(args.b); expect(args.s[0] == 'h'); expect(args.s[1] == 'i'); +} + {#code_end#} +

+ Anonymous structs can be created without specifying field names, and are referred to as "tuples". +

+

+ The fields are implicitly named using numbers starting from 0. Because their names are integers, + the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers. +

+

+ Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}. +

+ {#code_begin|test|tuple#} +const std = @import("std"); +const expect = std.testing.expect; + +test "tuple" { + const values = .{ + @as(u32, 1234), + @as(f64, 12.34), + true, + "hi", + } ++ .{false} ** 2; + expect(values[0] == 1234); + expect(values[4] == false); + inline for (values) |v, i| { + if (i != 2) continue; + expect(v); + } + expect(values.len == 6); + expect(values.@"3"[0] == 'h'); } {#code_end#} {#header_close#} -- 2.54.0