4.1 What is Ownership?

Ownership

Memory - Stack vs Heap

Stack Heap
Store fixed sized data Store dynamic sized data
Fixed size Dynamic in runtime
Stack frames Less organized
Faster Slower (Need time to looking for a place to store new data)
Variable lives only inside of the stack frame

Ownership Rules

  1. Each value in Rust has a variable that’s called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.

Move Ownership

fn main() {
    let x = 5;
    let y = x; //copy

    let s1 = String::from("hello");
    let s2 = s1; //move ownership
}

Make new heap allocation?

스크린샷 2023-01-27 오후 7.13.08.png

Expansive!

Especially when the data on the heap were large.

s2 = s1 could be very expensive in terms of runtime performance.

(allocation)

Make copy of pointers?