Ownership & Move Semantics
Flame achieves high performance and complete memory safety through a compile-time ownership model inspired by Rust. This eliminates both memory leaks and the unpredictable latency spikes of a Garbage Collector (GC).
The Three Rules of Ownership
Section titled “The Three Rules of Ownership”- Every value in Flame has a variable that is called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value is dropped automatically from memory (RAII).
Move Semantics
Section titled “Move Semantics”When a variable is assigned to another variable or passed by value to a function, ownership of the data is moved:
fn consume_data(s: String) { print($"Received: {s}")} // `s` is dropped here when the function exits
let original = "Hello Flame"
// Ownership is transferred (moved) to `consume_data`consume_data(original)
// ERROR: `original` was moved and is no longer valid!// print(original)Scope & Deterministic Drops (RAII)
Section titled “Scope & Deterministic Drops (RAII)”Memory is freed the moment an owner exits its lexical { ... } block:
{ let buffer = [1, 2, 3, 4, 5] print(buffer.len()) // 5} // `buffer` goes out of scope; heap allocation is freed immediately!
// `buffer` is inaccessible here