Learn Aero / References & Borrowing

References & Borrowing

Aero adopts an ownership + borrow-checking model similar to Rust: values use move semantics by default, and borrows come in immutable/mutable flavors.

Immutable Borrows &

fn read(p: &i64) -> i64 {
    return *p;   // 解引用
}

let x = 10;
print(read(&x));   // 10

Mutable Borrows &mut

To modify the caller's variable, pass a mutable reference:

fn bump(p: &mut i64) {
    *p = *p + 1;
}

let mut x = 10;
bump(&mut x);
print(x);   // 11

Basic Rules

  • At any moment there can be only one mutable borrow, or multiple immutable borrows (the borrow checker blocks conflicts at compile time)
  • Dereference with *r
  • A mutable borrow &mut requires the variable itself to be writable (let mut)
  • Why It's Needed

    Function arguments passed by value are copies; references "borrow" the caller's data without copying. Aero manages "who owns, who borrows" at compile time to avoid dangling pointers and dangling references.

    Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
    SereinTeam