authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-13 15:35:16-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-11-13 15:35:16-08:00
logbfcf18c5a7d13d990c6bbda04d6a2cd37038a9ea
treef75a1becd8f5df0e222a95d479ca7cbc260a1325
parent4fc295dc02dd0b311a22b3b8b962015138dde4c9

langref: delete misleading example code about packed structs


1 files changed, 10 insertions(+), 26 deletions(-)

doc/langref/test_structs.zig+10-26
......@@ -6,28 +6,13 @@ const Point = struct {
66 y: f32,
77};
88
9// Maybe we want to pass it to OpenGL so we want to be particular about
10// how the bytes are arranged.
11const Point2 = packed struct {
12 x: f32,
13 y: f32,
14};
15
169// Declare an instance of a struct.
17const p = Point{
10const p: Point = .{
1811 .x = 0.12,
1912 .y = 0.34,
2013};
2114
22// Maybe we're not ready to fill out some of the fields.
23var p2 = Point{
24 .x = 0.12,
25 .y = undefined,
26};
27
28// Structs can have methods
29// Struct methods are not special, they are only namespaced
30// functions that you can call with dot syntax.
15// Functions in the struct's namespace can be called with dot syntax.
3116const Vec3 = struct {
3217 x: f32,
3318 y: f32,
......@@ -46,7 +31,6 @@ const Vec3 = struct {
4631 }
4732};
4833
49const expect = @import("std").testing.expect;
5034test "dot product" {
5135 const v1 = Vec3.init(1.0, 0.0, 0.0);
5236 const v2 = Vec3.init(0.0, 1.0, 0.0);
......@@ -67,14 +51,14 @@ test "struct namespaced variable" {
6751 try expect(Empty.PI == 3.14);
6852 try expect(@sizeOf(Empty) == 0);
6953
70 // you can still instantiate an empty struct
71 const does_nothing = Empty{};
54 // Empty structs can be instantiated the same as usual.
55 const does_nothing: Empty = .{};
7256
7357 _ = does_nothing;
7458}
7559
76// struct field order is determined by the compiler for optimal performance.
77// however, you can still calculate a struct base pointer given a field pointer:
60// Struct field order is determined by the compiler, however, a base pointer
61// can be computed from a field pointer:
7862fn setYBasedOnX(x: *f32, y: f32) void {
7963 const point: *Point = @fieldParentPtr("x", x);
8064 point.y = y;
......@@ -88,8 +72,7 @@ test "field parent pointer" {
8872 try expect(point.y == 0.9);
8973}
9074
91// You can return a struct from a function. This is how we do generics
92// in Zig:
75// Structs can be returned from functions.
9376fn LinkedList(comptime T: type) type {
9477 return struct {
9578 pub const Node = struct {
......@@ -105,8 +88,7 @@ fn LinkedList(comptime T: type) type {
10588}
10689
10790test "linked list" {
108 // Functions called at compile-time are memoized. This means you can
109 // do this:
91 // Functions called at compile-time are memoized.
11092 try expect(LinkedList(i32) == LinkedList(i32));
11193
11294 const list = LinkedList(i32){
......@@ -139,4 +121,6 @@ test "linked list" {
139121 // instead of try expect(list2.first.?.*.data == 1234);
140122}
141123
124const expect = @import("std").testing.expect;
125
142126// test