XU XUv0.16.0

06 · Ownership and borrow

No GC. Passing a string by value moves it. &x shared; $&x exclusive (x must be $).

Source

@main

fn take(s string) {
    println(s)
}

fn len_of(s &string) int {
    << s.len
}

fn append_bang(s $&string) {
    s.push_str("!")
}

fn main() int {
    msg := "hello"
    println(len_of(&msg))
    println(msg)

    $owned := "hi"
    append_bang($&owned)
    println(owned)

    take(owned)
    // owned 已 move,不可再使用
    << 0
}

Build:

xu run examples/06_ownership_borrow/main.xu
# or: xu build examples/06_ownership_borrow/main.xu
# default artifact is ./main on mac/linux, ./main.exe on Windows

Run from the separate examples repository root; source files live under examples/.