Skip to content

Instantly share code, notes, and snippets.

@cimourdain
Last active April 29, 2020 12:10
Show Gist options
  • Save cimourdain/99e88920155635d5bd7aa028058df2f4 to your computer and use it in GitHub Desktop.
Save cimourdain/99e88920155635d5bd7aa028058df2f4 to your computer and use it in GitHub Desktop.
rust ownership usage cases table

Rust Ownership examples of usage table

Stack variable (Copy trait)

By variable

Function need to code
update internal variable update original variable
No No
fn example(some_int: i32) {
    println!("Value of some_int is {}", some_int);
}

fn main() {
    let x = 1;
    example(x);
    // x still in scope and equal to 1
    println!("x={}", x);
}
Yes No
fn example(mut some_int: i32) {
    some_int += 99;
	// value of some_int is updated to 100
    println!("Value is updated to {}", some_int);
}

fn main() {
    let x = 1;
    example(x);
    // x still in scope and equal to 1
    println!("x={}", x);
}
Yes Yes
fn example(mut some_int: i32) -> i32 {
    some_int += 99;
    println!("Value is updated to {}", some_int);
    some_int
}

fn main() {
    let mut x = 1;
    x = example(x);
    // x still in scope was updated by function to 100
    println!("Value is updated to {}", x);
}

By reference

Function need to code
update internal variable update original variable
No No
fn example(some_int: &i32) {
    println!("Value is {}", some_int);
}

fn main() {
    let x = 1;
    example(&x);
    // x still in scope and equal to 1
    println!("x={}", x);
}
Yes Yes
fn example(some_int: &mut i32) {
    *some_int += 99;
    println!("Value is updated to {}", some_int);
}

fn main() {
    let mut x = 1;
    example(&mut x);
    // x still in scope and was updated to 100
    println!("x={}", x);
}

Heap variable (non Copy trait)

By variable

Function need to code
update internal variable update original variable
No No
fn example(some_str: String) {
    println!("Value is {}", some_str);
}

fn main() {
    let s = String::from("Hello");
    example(s);
    // s was borrowed by example, it is not in scope
}
Yes No
fn example(mut some_str: String) {
    some_str.push_str(" world");
    println!("Value is {}", some_str);
}

fn main() {
    let s = String::from("Hello");
    example(s);
    // s was borrowed by example, it is not in scope
}
Yes Yes
fn example(mut some_str: String) -> String{
    some_str.push_str(" world");
    println!("Value is {}", some_str);
    some_str
}

fn main() {
    let mut s = String::from("Hello");
    s = example(s);
    // s was borrowed and returned by example, it is back in scope
    println!("updated value of s is {}", s);
}

By reference

Function need to code
update internal variable update original variable
No No
fn example(some_str: &String) {
    println!("Value is {}", some_str);
}

fn main() {
    let s = String::from("Hello");
    example(&s);
    // s is still in scope
    println!("s={}", s);
}
Yes Yes
fn example(some_str: &mut String){
    some_str.push_str(" world");
    println!("Value is {}", some_str);
}

fn main() {
    let mut s = String::from("Hello");
    example(&mut s);
    // s is still in scope and was updated by example to "hello world"
    println!("updated value of s is {}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment