Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Structs

struct — the open data record: all fields public, constructed only by literal, sharing its cell like every non-primitive.

Declaration

use ink::{ Logger };

struct Point {
    x: f32;
    y: f32;
}

struct Style {
    color: u32 = 0xff00ff;   // field initializer: literals may omit it
    width: f32 = 1;
}

entry fn main() {
    let log = Logger("t");
    let s = Style {};
    log.info(f"{s.color} {s.width}");
}
16711935 1

Reference semantics

A struct value is a heap cell handle (see By-reference and nullable): assignment, argument passing, and returning share the cell, and a mutation through any alias is visible through all of them.

use ink::{ Logger };

struct Point { x: i32; y: i32; }

entry fn main() {
    let log = Logger("t");
    let mut p = Point { x: 1, y: 2 };
    let q = p;                       // SHARE: q and p name one cell (O(1))
    p.x = 4;                         // q.x is 4 now — sharing is the law
    log.info(f"{q.x}");
}
4

Writing is gated by the mut-binding law (see Modules and visibility): field stores and mut self methods need a let mut binding (or a mut parameter). Parameters declare their intent: fn nudge(mut pt: Point) may write the caller’s point; fn length(pt: Point) -> f64 promises not to, and returns a fresh record instead:

use ink::{ Logger };

struct Point { x: i32; y: i32; }

fn nudged(pt: Point) -> Point {     // builds a NEW record
    return Point { x: pt.x + 1, y: pt.y };
}

entry fn main() {
    let log = Logger("t");
    let p2 = nudged(Point { x: 1, y: 2 });
    log.info(f"{p2.x} {p2.y}");
}
2 2

== on two struct values is a cell-identity test — q == p is true exactly when they name one cell; two separately built literals are never equal. Field-wise comparison is an interface-shaped contract of your own (spell a member that compares — see Interfaces and dispatch).

Construction — the literal, everywhere

Name { field: expr, .. } is the only construction. There is no new, no class methods, no type-call. The literal is available everywhere — function bodies and module-level let initializers alike — and it allocates the cell.

  • Fields may be given in any order, by name.
  • The every-field-covered law: a literal must cover every field — spelled in the literal, or covered by the field’s declared initializer (tag: str = "..."). Omitting a field that has no initializer is a compile error — missing field `b` in the `P` literal (the every-field-covered law) — the silent zero-fill default is gone.
  • A field spelled twice is a compile error — duplicate field `a` in the `P` literal — there is no last-wins.
  • An omitted field with an initializer takes it; {} with all-initialized fields is legal.
  • Field-init shorthand: a bare field name takes its value from the like-named binding in scope — Name { x, y } is exactly Name { x: x, y: y }.
use ink::{ Logger };

struct Point { x: i32; y: i32; }
struct Style { color: u32 = 0xff00ff; width: f32 = 1; }
struct Rect { min: Point; max: Point; }

entry fn main() {
    let log = Logger("t");
    let q = Point { x: 9, y: 9 };
    let s = Style {};                     // the declared initializers fill the fields
    let r = Rect { min: Point { x: 0, y: 0 }, max: q };  // max shares q's cell
    log.info(f"{s.color} {r.max.x}");
}
16711935 9
use ink::{ Logger };

struct Task { id: i32; title: str; done: bool }

entry fn main() {
    let log = Logger("sh");
    let id = 7;
    let t = Task { id, title: "Eat an apple", done: false };  // `id` is `id: id`
    log.info(f"{t.id} {t.title} {t.done}");
}
7 Eat an apple false

All fields public, always

A struct is an open data record: member visibility in a struct body is a compile error (privacy needs construction control, which is the class’s job — see Classes and constructors). Structs also have no static members.

Methods live in impl blocks

A struct body is fields only — a fn member in the body is a hard parse error. Methods live in the inherent impl S { .. }, in the type’s module only — the one impl form (see Interfaces and dispatch):

impl Point {
    fn dist(self, other: Point) -> f32 { .. }   // inherent — the type's module
}

impl Point {
    fn hash(self) -> u64 { .. }                 // a member set, if you want one:
    fn eq(self, other: Point) -> bool { .. }    // satisfaction reads these
}

Methods carry real visibility — the class rule: pub fn exports cross-module, plain fn is module-private. Self spells the struct, in signatures (-> Self) and the literal (Self { x: 1, y: 2 }) alike:

use ink::{ Logger };

struct Counter { n: i32 }

impl Counter {
    pub fn new() -> Self { return Counter { n: 0 }; }
    fn bump(mut self) -> Self { self.n += 1; return self; }
    fn value(self) -> i32 { return self.n; }
}

entry fn main() {
    let log = Logger("t");
    let c = Counter::new().bump().bump();
    log.info(f"{c.value()}");
}
2

Free functions over data remain the default idiom; methods are for tight helpers, impl blocks for the members other code observes.

Limits, exhaustively:

  • fields are always public — no field-visibility dial (privacy needs construction control, which is the class’s job — see Classes and constructors);
  • construction is the literal — Point { x: 1, y: 2 } everywhere (open literal vs class-method-gated is the struct/class distinction).

Everything else class-shaped is allowed — impl blocks, Self, [disposal]. And the old “no destructor” limit is gone: a shared value dies exactly when its cell’s refcount reaches zero, so cleanup is one impl away — mark a [disposal] member on the type and the engine calls dispose at that moment (see Rc, dispose, and identity).

Interfaces and representation

  • Widening a struct to an interface I attaches the itable to the same handle — no allocation, no copy: the interface-typed value aliases the record, and mutations through it are visible to every other handle.
  • Representation: one slot per field inside the cell, in declaration order — primitive fields widened into their slot, composite fields as cell-handle slots (see Reified types and layout). Recursive shapes (next: ?Node) are legal because composite fields are pointer-sized.
  • Structs are for small data (points, rects, colors, configs), but any size is allowed.