官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 陆川县| 江华| 栖霞市| 张北县| 浮山县| 扶余县| 措勤县| 历史| 鹤庆县| 柘城县| 罗山县| 鹤岗市| 麻栗坡县| 五华县| 嵊泗县| 石景山区| 定结县| 保康县| 乌恰县| 揭西县| 蓬莱市| 芜湖市| 宜兰市| 古丈县| 邹城市| 镇江市| 吉木乃县| 湖南省| 贵德县| 自贡市| 阳春市| 通渭县| 新乡市| 兴安县| 嘉黎县| 宁夏| 柳江县| 榆林市| 通渭县| 内丘县| 鹤岗市|