--- title: What Is Ownership? tags: Note --- ## Stack and Heap ### Stack 疊盤子,先進後出(First in, last out) :::success 盤子的大小需一樣 --> 記憶體申請後就不再改變SIZE ::: ### Heap > When you enter, you state the number of people in your group, and the staff finds an empty table that fits everyone and leads you there. If someone in your group comes late, they can ask where you’ve been seated to find you. 進入餐廳就跟電腦申請記憶體一樣,會由服務員帶位(申請記憶體) :::success 當有一組客人的朋友後來才來,那麼服務生只需要帶位到該朋友的座位就好(**Memery is flexible**) ::: ## Literal String ```rust let s = "hello"; ``` 這種類型的字串是以Stack的方式將文字除純在記憶體中,在`rust`中這種儲存方式在走出程式碼區塊時就會Invalid ```rust { // s is not valid here, it’s not yet declared let s = "hello"; // s is valid from this point forward // do stuff with s } // this scope is now over, and s is no longer valid ``` ## Refs [Understanding Ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)