Skip to content

Values

Everything is a value. What owns a buffer has one owner, a second name moves it, and a copy is a `.clone()` you can see.

Atoll has one category of thing: values. There is no new, no box, no allocator argument, and no retain or release in source. A Point is a value, and so is a List[int] — the difference is that one of them owns a buffer, and owning a buffer is what makes a second name interesting. When you want an address, that is ^T — still a value, one that names a place.

Every example on this page is compiled and executed by the compiler’s own test suite, and its heap is checked back to zero.

A scalar copies

int, float, bool, char, byte and the sized integer types are pure values. A second name is an independent copy.

fn main() int {
    a: int = 3
    mut b: int = a
    b = 10
    return a * 100 + b       // 3 and 10 — `a` did not move
}

An aggregate of scalars copies

A struct or tuple whose fields are all scalars owns no buffer, so it follows the scalar rule exactly.

struct Point { x int, y int }

fn main() int {
    mut a: Point = Point { x: 3, y: 0 }
    mut b: Point = a
    b.x = 10
    return a.x * 100 + b.x
}
fn main() int {
    mut a: (int, int) = (3, 4)
    mut b: (int, int) = a
    b.0 = 10
    return a.0 * 100 + b.0
}

A second name takes what owns a buffer

A List, a string, a Map, and a struct or tuple holding one — anything that owns a buffer — is still a value, and it has one owner. Binding it to a second name moves it: the value’s own bytes go to the new name — a list’s sixteen-byte header, a struct’s fields — and the buffer is never copied:

fn main() int {
    xs: List[int] = [1, 2, 3]
    ys: List[int] = xs        // a move: `xs` is gone
    return ys.len()
}

The first name is gone, and reading it is ATOLL3102:

fn main() int {
    xs: List[int] = [1, 2, 3]
    ys: List[int] = xs
    return xs.len() + ys.len()
}

A read counts only where a run can reach it. Code past a return, a break or a continue, or past a loop with no break, reads nothing, so a moved name there is no read after a move, and a move there moves nothing. A name mentioned there is still held to its brace.

fn take(s string) int { return s.len() }

fn main() int {
    s := "ab"
    mut i := 0
    for i < 3 {
        i += 1
        break
        take(s)             // no run gets here, so `s` is not moved
    }
    return i + s.len()
}

front-run=3 marks an answer the new compiler gives. Today’s compiler refuses it (ATOLL3102), for a move no run makes.

The same rule covers every place a value is handed on: an assignment, a field of a struct literal, an element of a list, m.put(key, value), an argument to a parameter that owns it. Three things say what you want instead, and each says what it costs:

  • .clone() makes a second value, with a buffer of its own.
  • A &T parameter reads the value and leaves it with the caller.
  • A ^T is how two names share one value; see Pointers.

There is no fourth: a buffer is never copied, or shared, behind your back.

A loop’s next turn

A loop’s next turn reads what the last one left. A value a turn moves out of a name from outside the loop is gone when the body runs again. So the move is refused unless the turn gives the name a new value before it ends, or leaves the loop:

fn take(xs List[int]) int { return xs.len() }

fn main() int {
    xs: List[int] = [1, 2, 3]
    mut n := 0
    for n < 6 {
        n += take(xs)      // the second turn reads what the first moved
    }
    return n
}
fn take(xs List[int]) int { return xs.len() }

fn main() int {
    mut xs: List[int] = [1, 2, 3]
    mut n := 0
    for n < 6 {
        n += take(xs)
        xs = [1, 2, 3]     // a new value before the next turn
    }
    return n
}

After the loop, a name that a turn or a break moved is gone, as it is after an if with an arm that moved it.

Reads borrow

xs[i], m.get(k), p.name, a window such as s[1..], and the element a for loop yields are reads, and a read borrows: no count is taken and nothing is copied. A name bound to a read is a borrow too, so it can be read as often as you like, for as long as what it reads does not change:

fn main() int {
    xs: []string = ["a", "bb"]
    s := xs.get(1) ?? ""
    return s.len() * 10 + (xs.get(0) ?? "").len()   // 2 and 1
}

A parameter owns what it is handed

A parameter written T owns its argument, as a binding does, and the caller’s name is gone. A parameter that only reads says so with &T, and the call looks the same; a function may then be called with the same value twice:

fn count(xs &List[int]) int { return xs.len() }

fn main() int {
    xs: List[int] = [1, 2, 3]
    return count(xs) + xs.len()
}

Without the &, that program is refused:

fn count(xs List[int]) int { return xs.len() }

fn main() int {
    xs: List[int] = [1, 2, 3]
    return count(xs) + xs.len()
}

A value that owes a drop and carries no count — a Drop-declaring struct, an open file, a task — has the same rule and no .clone() to reach for; see Destruction.

clone() when you want two values

fn main() int {
    mut xs: List[int] = [1, 2, 3]
    mut ys: List[int] = xs.clone()
    ys.set(0, 99)
    a: int = match xs.get(0) { Some(v) => v None => 0 - 1 }
    b: int = match ys.get(0) { Some(v) => v None => 0 - 1 }
    return a * 100 + b       // 1 and 99 — independent
}

clone() is a Clone method, and you do not write it for a type of your own. A struct, a tuple and an enum clone from what they are made of, member by member, as they compare. A ^T member is copied as a pointer and stays shared. A type that owes a drop has no clone unless you write impl Clone for it, and neither has a type holding one.

Mutation

mut is a permission on a binding. Without it, a write is rejected at check time.

struct Counter {
    value int

    fn bump(&mut self) void { self.value = self.value + 1 }
}

fn main() int {
    mut c: Counter = Counter { value: 0 }
    c.bump()
    c.bump()
    return c.value
}

On a parameter, mut is the same word: a writable place in the callee. It does not write the caller’s argument slot: fn twice(mut n int) writes its own n, and twice(a) leaves a as it was. Writing the caller’s value is n &mut int. Today’s compiler still refuses the spelling (ATOLL1063); there scratch is a local:

fn twice(n int) int {
    mut scratch: int = n
    scratch = scratch * 2
    return scratch
}

fn main() int {
    a: int = 21
    b: int = twice(a)
    return a * 1000 + b      // 21 and 42 — the caller's `a` is untouched
}

To write the caller’s value, take a mut pointer and pass &place. See Pointers. &mut self is the method form of the same rule: c.bump() still writes c.

Equality is by value

== and != dispatch through Equatable, and structs get a field-wise implementation without you writing one. Strings and lists compare by content, never by which buffer they hold.

struct Point { x int, y int }

fn main() int {
    mut n: int = 0

    p: Point = Point { x: 1, y: 2 }
    q: Point = Point { x: 1, y: 2 }
    if p == q { n = n + 100 }

    a: string = "a long value here"
    b: string = "a long value here"
    if a == b { n = n + 10 }

    xs: List[int] = [1, 2, 3]
    ys: List[int] = [1, 2, 3]
    if xs == ys { n = n + 1 }

    return n
}

Write your own Equatable when equality is narrower than “all fields match” — for example when one field is a cache. A Hashable type must keep hash_code() consistent with equals: two equal values that hash differently will miss in a map. A type that writes its own equals is therefore a map key only once it writes its own hash_code too.

Never infer identity from an address. == on a ^T compares pointees; pointer.same(p, q) (module pointer) is the one thing that compares addresses.

A change to a list ends the references into it

There is one reference count per block, so a reference into a container keeps the whole buffer alive. It also borrows the container, and any change to the list ends that borrow: growing it, xs[i] = v, a push within capacity. A use of the reference past the change is refused (ATOLL1097), to read or to write. It would name a block xs no longer reads.

fn main() int {
    mut xs: []int = [1, 2, 3]
    p := &xs.get(1)
    xs[2] = 7                  // the borrow ends here
    match p { Some(mut q) => { q.^ = 9 } None => {} }
    return xs.get(1) ?? 0
}

Use the reference before the list changes, or take it again after:

fn main() int {
    mut xs: []int = [1, 2, 3]
    mut p := &xs.get(1)
    xs[2] = 7
    p = &xs.get(1)                 // a fresh borrow
    match p { Some(mut q) => { q.^ = 12 } None => {} }
    return xs.get(1) ?? 0
}

The rule is one count per block, and it reaches list views the same way — Lists for a view, and References for why a reference holds a whole buffer rather than an element.

Destruction

When a value is not named again, its buffer is released. This covers normal exits, branches, early returns, error propagation, resumable frames and cancelled tasks. You do not write a free.

Where the release lands is not “at the end of scope”. A value nothing mentions again is released on the spot; a value something later mentions is held to the end of its block. That difference is invisible for memory and very visible for a Drop body, so Destruction states it with the measurements.

A type implementing Drop gets a body at that moment:

@host fn record(n int) void

struct Handle { id int }

impl Drop for Handle {
    fn drop(&self) void { record(self.id) }
}

fn main() int {
    h: Handle = Handle { id: 7 }
    record(1)
    return h.id              // records 1, then 7 as `h` is destroyed
}

The records=1,7 on that block is checked: the book asserts the call log, not just the answer. A Drop body that never runs and one that runs twice leave the answer and the heap identical, so nothing else would notice.

Drop is for resource semantics — closing a handle, releasing a lease — not for memory, which is already handled.

Cycles need no annotation

Reference counting alone cannot reclaim a cycle. Atoll collects them by trial deletion rather than by asking you to declare where one might form: a decrement that does not reach zero makes the block a candidate, and a later scan decides whether the only references keeping it alive come from inside the cycle.

There is no @weak, no capture annotation and no cycle diagnostic. You write ordinary Atoll and cycles are collected. See Cycles for what puts a collection at a boundary, and Ownership for how the scan works.

Writing @weak is refused rather than ignored, because a decorator that parses and does nothing reads as a weak edge that is not there:

struct Node {
    v int
    @weak parent Node?
}

fn main() int {
    n := Node{ v: 7, parent: None }
    return n.v
}

What you never write

Elsewhere Atoll
new, an allocator argument construction is a literal
Box, Rc, Arc ^T, when you want an address at all
&T / &mut T borrows of a parameter, written on the type; see Pointers
a lifetime annotation escaping a frame is refused, not annotated
@weak to break a cycle nothing — cycles are collected

Iteration of a collection borrows it: for v in xs reads the collection, does not move it, and may not change it until the walk is over. See Loops.

Type to search…

↑↓ navigate↵ selectEsc close