Docs / Tutorial
06 · Ownership and borrow
No GC. Passing a string by value moves it. &x shared; mut &x exclusive (x must be mut).
Source
@main
fn take(s string) {
println(s)
}
fn len_of(s &string) int {
<< s.len
}
fn append_bang(s &mut string) {
s.push_str("!")
}
fn main() int {
msg := "hello"
println(len_of(&msg))
println(msg)
mut owned := "hi"
append_bang(mut &owned)
println(owned)
take(owned)
// owned 已 move,不可再使用
<< 0
}Build (from the language repo root; swap the binary for your platform):
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/.