Skip to main content

Ownership & Borrowing

RGC uses an ownership model to manage memory: every value has a unique owner and is freed when it goes out of scope; borrowing (@ / @var) provides safe shared access. No GC, no runtime overhead.

Ownership​

After a value is moved, the original variable can no longer be used:

def main() {
a = "hello"
b = a # ownership moves from a to b
print(b) # ok
# print(a) # error: a has been moved
}

Read-only borrowing @​

Use @ to borrow a value without transferring ownership:

def length(s: @str) -> int {
return s.len()
}

def main() {
text = "RGC"
print(length(@text)) # read-only borrow
print(text) # text is still usable
}

Mutable borrowing @var​

Use @var when you need to modify a value:

def push_zero(lst: @var List[int]) {
lst.push(0)
}

Method calls​

Container methods handle borrowing automatically:

def main() {
list = [1, 2, 3]
list.push(4) # mutable borrow
n = list.len() # read-only borrow
}

Memory safety guarantees​

Ownership and borrow checking happen at compile time:

  • No use-after-move
  • No dangling references
  • No data races (mutable and shared borrows are exclusive)
  • No GC pauses; predictable performance

Continue reading​