当前位置:网站首页>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
边栏推荐
- Using files to save data (C language)
- [UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
- Go语言JSON包使用
- An example of linear regression based on tensorflow
- Clion installation tutorial
- 2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
- Qt读写XML文件(含源码+注释)
- Build openstack platform
- cv_ Solution of mismatch between bridge and opencv
- Calculation of fishing net road density
猜你喜欢

.104History

Batch export ArcGIS attribute table

Re expression régulière

MySQL 中的字符串函数
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)](/img/91/3272d5ad04cd1d8476c739546f4356.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)

Classification of cifar100 data set based on convolutional neural network
![[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)](/img/07/4814eb203dcca59416a7997bbedbf6.png)
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)

Qtablewidget usage explanation

Cross domain settings of Chrome browser -- including new and old versions

.105Location
随机推荐
Generate verification code
Go对文件操作
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
Secure credit
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
Utilisation de la liste - Ajouter, supprimer et modifier la requête
587. Install fence / Sword finger offer II 014 Anagrams in strings
cartographer_ There is no problem compiling node, but running the bug that hangs directly
Crawling mobile game website game details and comments (MQ + multithreading)
Docker 安装 Redis
I/O多路复用及其相关详解
C language loop structure program
Array rotation
C1 notes [task training part 2]
MySQL 中的字符串函数
Realization of consumer gray scale
YOLOv4剪枝【附代码】
String function in MySQL
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
Data stream encryption and decryption of C