当前位置:网站首页>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
边栏推荐
- Halo open source project learning (II): entity classes and data tables
- C byte array (byte []) and string are converted to each other
- The ultimate experience, the audio and video technology behind the tiktok
- 587. Install fence / Sword finger offer II 014 Anagrams in strings
- Generate verification code
- MySQL 中的字符串函数
- Implementation of k8s redis one master multi slave dynamic capacity expansion
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- C [file operation] read TXT text by line
- I/O多路复用及其相关详解
猜你喜欢
Cross domain settings of Chrome browser -- including new and old versions
re正则表达式
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
Welcome to the markdown editor
JS high frequency interview questions
Go file operation
Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
Re regular expression
Auto. JS custom dialog box
Re expression régulière
随机推荐
Go语言JSON包使用
Summary of common server error codes
列錶的使用-增删改查
ArcGIS license error -15 solution
Pyppeter crawler
极致体验,揭晓抖音背后的音视频技术
Stanford machine learning course summary
Transfer learning of five categories of pictures based on VGg
读取excel,int 数字时间转时间
Special effects case collection: mouse planet small tail
C1 notes [task training chapter I]
MySQL auto start settings start with systemctl start mysqld
C language array processing batch data
Batch export ArcGIS attribute table
Re expression régulière
Flash - Middleware
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
Timestamp to formatted date
7-21 wrong questions involve knowledge points.
Crawl lottery data