wiki/dev/lang/rust/Borrow.md

571 B

Memory borrowing.

Reference.

To pass an immutable reference, use & sign.

let s1 = String::from("hello");
foo(&s1);

Mutable reference.

Use &mut to pass mutable reference. You can have only one mutable reference at a time.

let s1 = String::from("world");
foo(&mut s1)

Ownership.

You pass ownership by default: no special syntax.

let s1 = String::from("hi");
foo(s1) // foo() now owns s1 and s1 is no longer accesible after this call.
fn foo() -> String
{
  let s1 = String::from("wow");

  return s1;
}