image

fn main() {
    let s = String::from("hello");
    let len = calculate_length(&s);
    println!("The length of '{}' is: {}", s, len);
}

fn calculate_length(s: &String) -> usize {
    s.len()
}

Change value references

fn main() {
    let s = String::from("hello");
    change(&s);
}

fn change(some_string: &String) -> String {
    some_string.push_str(", world!");
}
// Doesn't work!

Mutable References

fn main() {
    let mut s = String::from("hello");
    change(&mut s);
}

fn change(some_string: &mut String) {
    some_string.push_str(", world!");
}
// It works like a charm!