Borrowing
Temporary access with &T and &mut T.
Mutable borrow
Only one mutable borrow at a time. Finish using it before returning to the owner.
fn main() {
mut x = 1
let r = &mut x
print(*r)
unsafe {
*r = 5
}
print(x)
}fn main() -> void {
mut x = 1
let r = &mut x
print(*r)
unsafe {
*r = 5
}
print(x)
}Output
1
5
Shared read-only borrow
Multiple &T borrows are allowed together.
fn main() {
let msg = "hello"
let r = &msg
print(*r)
print(msg)
}fn main() -> void {
let msg: string = "hello"
let r = &msg
print(*r)
print(msg)
}Output
hello
hello