Structs

Contents

Structs#

Three forms.

Named-field struct.

struct User { id: u32, name: String }
let u = User { id: 1, name: "rk".into() };

Tuple struct.

struct Point(i32, i32);
let p = Point(3, 4);

Unit struct; no data, just a type tag.

struct Marker;

Struct update syntax keeps the original fields and overrides some.

let v2 = User { name: "operator".into(), ..u };

References#

  • OOP for methods, traits, and impl blocks.

  • Tuples for the anonymous-field sibling.