Structs
Basic structs
tape
struct Point {
x: f64;
y: f64;
}
let origin = Point { x: 0.0, y: 0.0 };Field separators accept both , and ; in declarations and literals.
Field access
tape
let px = origin.x;Methods
Use Type.name syntax to declare methods:
tape
struct Vec2 { x: f64; y: f64; }
fn Vec2.length(*self) -> f64 {
return math.sqrt(self.x * self.x + self.y * self.y);
}
fn Vec2.normalize(*self) -> Vec2 {
let len = self.length();
return Vec2 { x: self.x / len, y: self.y / len };
}The first parameter is self (by value) or *self (by pointer).
Swizzle access
Structs can declare swizzle patterns with @swizzle to enable GLSL-style field access:
tape
@swizzle xy: Vec2;
@swizzle xyz: Vec3;
struct Vec4 { x: f64; y: f64; z: f64; w: f64; }
let v = Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 };
let xy = v.xy; // Vec2 { x: 1.0, y: 2.0 }
let zw = v.zw; // Vec2 { z: 3.0, w: 4.0 }The @swizzle attribute maps a field-name pattern to a result type. It cannot be used with @packed or @opaque structs.
Struct attributes
tape
@packed
struct PacketHeader {
magic: u32;
length: u16;
flags: u8;
}
@align(16)
struct SimdVec {
data: [4]f32;
}
@opaque
struct Handle {
id: u64;
}@packed— no padding between fields (alignment 1)@align(N)— minimum alignment requirement@opaque— fields not visible outside the module
Last modified: