当前位置:网站首页>Rust advanced ownership - memory management
Rust advanced ownership - memory management
2022-06-27 19:57:00 【User 3147702】
1. introduction
In the previous article , We introduced Rust The ownership of the :
Ownership mechanism Rust It can conveniently realize the automatic recycling of memory , however Rust How to divide and manage memory ? This article will introduce .
2. Memory allocation -- Heap and stack
Like many other languages ,Rust The memory is also divided into heap and stack . because Rust Language is a system level programming language , In the process of writing, we must know whether the memory is allocated to heap space or stack space , But usually , Placing data on the stack is not called “ Distribute ”, It is “ Push the ”.
stay Rust in , Only data of fixed size known at compile time will be allocated in stack space , Data that cannot be sized at compile time , Can only be placed in heap space .
For example ,Rust There are two types of strings in :
// &str type
let str1 = "hello world!";
// String type
let str2 = String::from("hello");str1 yes &str type , Its value is fixed in size and immutable in content , He can already determine the size of the memory used at compile time , therefore ,str1 Will be pushed onto the stack .
str2 yes String type , This is a kind of Rust The packaging type of , It is a variable string type , for example , You can use the following methods to str2 Add new content :
str2.push_str(" world!");therefore ,String Type of str2 Is allocated in heap space , For all that , actually , A structure will still be pushed into the stack space , Used to hold pointers to heap space 、 The amount of heap space allocated this time , And used length .
3. Memory release
Because heap space is dynamically allocated at runtime , So like many other languages , Heap space cleaning is also an issue we need to consider ,Rust The ownership mechanism of has solved this problem to a great extent .
When a variable is out of scope , According to the ownership mechanism ,Rust Will automatically call a file named drop The special function of , In this function ,Rust Will release all memory that is not being used anymore .
In the previous article , We introduced Rust Of “ Move ” Mechanism , In a nutshell , For the basic type , When a variable is assigned to another variable ,Rust Will open up a new space in the stack space for new variables , Make a copy of the original value , This makes both variables available in the current scope , here “ Basic data type ” Only include :
- All integer types , Include symbols 、 Unsigned type , Such as u32、i64 etc. ;
- Boolean type bool;
- Character type char.
For data allocated in heap space , When a variable is assigned to another variable ,Rust Will destroy the original variable , Ownership of the data is Move To the new variable .
Why is this difference ? Suppose you keep a copy of the variable contents pointing to the data in the heap space and give it to the new variable , Then there will be a case where the heap data is pointed to by two pointers . When two variables exit the scope ,Rust You must decide how many times you want to release the heap data , And in this case , Modifying a variable means that the actual data pointed to by another variable has been modified , The behavior is still different from the base type . in fact , The best solution is to replicate the data in the heap space synchronously , In many other languages “ Deep copy ”, However, the performance will be greatly affected . therefore , After weighing ,Rust adopt “ Move ” To realize the assignment of heap space variables .
边栏推荐
- Cdga | what is the core of digital transformation in the transportation industry?
- Talk about graduation season
- 过关斩将,擒“指针”(下)
- OpenSSL client programming: SSL session failure caused by an obscure function
- Tupu digital twin intelligent energy integrated management and control platform
- Labelimg usage guide
- 1028 List Sorting
- 【help】JVM的CPU资源占用过高问题的排查
- MySQL初学者福利
- Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
猜你喜欢

MySQL beginner benefits

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning

UE4实现长按功能

数智化进入“深水区”,数据治理是关键

Redis持久化

Mathematical derivation from perceptron to feedforward neural network

指针和结构体

过关斩将,擒“指针”(下)

External interrupt experiment based on stm32f103zet6 library function
随机推荐
(LC)46. 全排列
【云驻共创】 什么是信息化?什么是数字化?这两者有什么联系和区别?
# Leetcode 821. 字符的最短距离(简单)
券商经理的开户二维码开户买股票安全吗?有谁知道啊
Code and principle of RANSAC
作为软件工程师,给年轻时的自己的建议(下)
刷题笔记-树(Easy)-更新中
Pointers and structs
作用域-Number和String的常用Api(方法)
Function key input experiment based on stm32f103zet6 Library
流程判断-三目运算-for循环
Kotlin微信支付回调后界面卡死并抛出UIPageFragmentActivity WindowLeaked
(LC)46. Full Permutation
网络上开户买股票是否安全呢?刚接触股票,不懂求指导
Accumulating power in the middle stage, UFIDA IUAP builds a new base for digital intelligence of social enterprises
Golang map 并发读写问题源码分析
实战回忆录:从Webshell开始突破边界
ABAP-SM30删除前检查
可靠的分布式锁 RedLock 与 redisson 的实现
Bit. Store: long bear market, stable stacking products may become the main theme