Learn about borrow, ‘&’, and accessing the heap in Rust Programming Language

In this example we access a string (stored on the heap) and modify it using the & to borrow it and update it.

Integers can be modified on the stack, but a String is an Array of unknown length so it has to live on the heap…

Using “Borrow” &

Get the Rust Example Code:

/* Rust learning : Day 2 */

fn main() {

    // demo 'borrow'

    let mut mhouse = String::from("detached");
    
    make_semi(&mut mhouse);

    println!("{:?}", mhouse);

}

fn make_semi(house : &mut String){
    house.push_str(" - semi");
}

❯ cd "/home/rag/moo3/example/src/" && rustc main.rs && "/home/rag/moo3/example/src/"main
"detached - semi"
❯ pwd
/home/rag/moo3/example/src
❯ tree
.
├── main
├── main.rs
└── tempCodeRunnerFile.rs

0 directories, 3 files