- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 145字
- 2021-07-02 23:07:26
Let's consider the heap
As already discussed, the heap is typically used for complex types. The stack frame model can still be used, but it will need modifying, as the stack will need to point to the base address of the complex type on the heap.
Let's construct a stack frame for the following piece of code:
fn main() { let f = 42; let my_ids: Vec<i64> = Vec::with_capacity(5); }

Space is allocated correctly for f, but my_ids is different; it is a Vector<i64> with pre-allocated space for five i64s values. While the vector itself is stored in the stack, its contents are allocated in the heap.
Values in the heap are considered to be more persistent than those in the stack. That means, unlike values in the stack, their lifetime does not have to be as short as the block's they were defined in.
推薦閱讀
- Learning ROS for Robotics Programming(Second Edition)
- 算法零基礎(chǔ)一本通(Python版)
- Linux C/C++服務(wù)器開(kāi)發(fā)實(shí)踐
- 垃圾回收的算法與實(shí)現(xiàn)
- Java從入門(mén)到精通(第4版)
- Java從入門(mén)到精通(第5版)
- 云原生Spring實(shí)戰(zhàn)
- Kotlin Standard Library Cookbook
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第2版)(微課版)
- Android驅(qū)動(dòng)開(kāi)發(fā)權(quán)威指南
- Red Hat Enterprise Linux Troubleshooting Guide
- Hadoop 2.X HDFS源碼剖析
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)APP設(shè)計(jì)
- Natural Language Processing with Python Quick Start Guide
- Delphi開(kāi)發(fā)典型模塊大全(修訂版)