Structs

Structs are plain data types with named fields.

struct Point {
  x: int
  y: int
}

struct User {
  name: str
  age: int
}

Functions that take structs can read their fields.

fn distance(a: Point, b: Point) -> int {
  dx = a.x - b.x
  dy = a.y - b.y
  dx * dx + dy * dy
}

fn main() {
  p1 = Point { x: 1, y: 2 }
  p2 = Point { x: 4, y: 6 }

  println(p1.x)
  println(distance(p1, p2))

Mutating fields requires mut.

  mut u = User { name: "alice", age: 30 }
  u.age = 31
  println("{u.name} is {u.age}")
}
$ pyr run structs.pyr
1
25
alice is 31