当前位置:网站首页>RC smart pointer in rust
RC smart pointer in rust
2022-04-23 18:03:00 【Xu Yeping】
Rust Medium Rc Intelligent pointer
1 First time to know Rc
Let's start with a simple example :
use std::rc::Rc;
fn main() {
let x = Rc::new(123);
println!("{:?}", x);
}
---------------------------------------------------
cargo run
123
This with Box The pointer is almost the same , The difference between them is clone()
Specific implementation process of .
2 clone() Method
See the following example :
use std::rc::Rc;
fn main() {
let x = Rc::new(123);
let y = x.clone();
println!("{:?}, {:?}", x, y);
}
---------------------------------------------------
cargo run
123, 123
Look and Box Do not have what difference . We can Rc Switch to Box, The result is the same . The difference between them is the internal execution process .Rc This clone(), There is no real replication of data , It's an address that shares the same data , And use an internal counter to record the number of references . Every time you execute clone() When the method is used , Add one counter , Every time you execute drop() When the method is used , Counter minus one . Once the counter is zero , Then clean up the actual data .
Personal feeling , This mechanism is better than C++ Smart pointers are easy to implement , Easier to understand , It's less error prone to use . I'll write an article about C++ Comparison of intelligent pointer implementation mechanism .
版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498606.html
边栏推荐
- Process management command
- Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
- Special effects case collection: mouse planet small tail
- Go语言JSON包使用
- An example of linear regression based on tensorflow
- MySQL_ 01_ Simple data retrieval
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- C network related operations
- Flash - Middleware
- String function in MySQL
猜你喜欢
Operation of 2022 mobile crane driver national question bank simulation examination platform
Jenkspy package installation
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
Batch export ArcGIS attribute table
C1 notes [task training chapter I]
Romance in C language
7-21 wrong questions involve knowledge points.
Dock installation redis
Docker installation MySQL
随机推荐
Crawling mobile game website game details and comments (MQ + multithreading)
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
String function in MySQL
ES6 face test questions (reference documents)
C#的随机数生成
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
Data stream encryption and decryption of C
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
cv_ Solution of mismatch between bridge and opencv
Installation du docker redis
Go file operation
Timestamp to formatted date
Eigen learning summary
消费者灰度实现思路
极致体验,揭晓抖音背后的音视频技术
Install pyshp Library
Docker 安裝 Redis
Crawl lottery data
Go language JSON package usage
C1小笔记【任务训练篇二】