当前位置:网站首页>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 .
边栏推荐
- ABAP-CL_OBJECT_COLLECTION工具类
- One to one relationship
- What is ssr/ssg/isr? How do I host them on AWS?
- shell脚本常用命令(四)
- Memoirs of actual combat: breaking the border from webshell
- Labelimg usage guide
- 从指令交读掌握函数调用堆栈详细过程
- crontab的学习随笔
- Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
- 刷题记录:Easy 数组(持续更新)
猜你喜欢
Erreur Keil de Huada Single Chip Computer La solution de Weak
Adding, deleting, modifying and querying MySQL tables (basic)
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
在线文本按行批量反转工具
多伦多大学博士论文 | 深度学习中的训练效率和鲁棒性
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
拥抱云原生:江苏移动订单中心实践
“我让这个世界更酷”2022华清远见研发产品发布会圆满成功
MySQL初学者福利
键入网址到网页显示,期间发生了什么?
随机推荐
Array exercises follow up
Blink SQL built in functions
爬取国家法律法规数据库
UE4:Build Configuration和Config的解释
One to one relationship
作为软件工程师,给年轻时的自己的建议(下)
shell脚本常用命令(四)
Leetcode 1381. 设计一个支持增量操作的栈
判断一个变量是数组还是对象?
通过 Cargo 管理 Rust 项目
现在网上买股票开户身份证信息安全吗?
数仓的字符截取三胞胎:substrb、substr、substring
过关斩将,擒“指针”(下)
1023 Have Fun with Numbers
作用域-Number和String的常用Api(方法)
Redis持久化
GIS遥感R语言学习看这里
Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
1024 Palindromic Number
【登录界面】