Skip to content
La3 Docs
Browse docs

Data Structures

Structs, enums, patterns, lists, maps, sets, tuples, and arrays.

Structs

Structs separate data layout from behavior so the reader can see shape first and methods second.

Construction uses named fields, and update syntax copies the remaining fields from an existing value.

  • struct owns the data layout.
  • impl owns methods and constructors.
  • self, &self, &mut self, and mut self each encode a different ownership story.
struct Point {
    x: f64,
    y: f64,
}

impl Point {
    fn distance(&self, other: &Point) -> f64 {
        let dx = self.x - other.x
        let dy = self.y - other.y
        (dx * dx + dy * dy) ** 0.5
    }
}

Enums and patterns

Enums are sum types with data-carrying variants, which is the central Rust idea La3 adopts almost directly.

Pattern matching makes the enum shape visible in control flow and forces exhaustiveness when the type has known variants.

  • Variants can be tuple-like or struct-like.
  • Patterns can destructure, guard, bind, and match ranges.
  • Option<T> and Result<T> are ordinary enums with especially important semantics.
enum Shape {
    Circle(f64),
    Rect { width: f64, height: f64 },
}

Collections

Lists, maps, and sets each own memory and each get a simple, visible API.

  • List<T> grows, shrinks, and maps over owned elements.
  • Map<K, V> stores key-value pairs and exposes lookup plus mutation methods.
  • Set<T> models membership, not ordering.
  • The literal forms keep collection examples compact without hiding ownership.

Tuples and arrays

Tuples model fixed heterogeneous groupings, and arrays model fixed homogeneous storage.

  • Tuples are usually for multi-value returns and small grouped values.
  • Arrays carry their length in the type.
  • Slices are borrowed windows into arrays or lists.