当前位置:网站首页>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
边栏推荐
- Reptile efficiency improvement method
- Crawl the product data of Xiaomi Youpin app
- Arcpy adds fields and loop assignments to vector data
- Tensorflow tensor introduction
- Summary of floating point double precision, single precision and half precision knowledge
- Docker 安装 Redis
- Batch export ArcGIS attribute table
- Random number generation of C #
- Nat commun | current progress and open challenges of applied deep learning in Bioscience
- C# 网络相关操作
猜你喜欢
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)](/img/74/bb173ca53d62304908ca80d3e96939.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)

Jenkspy package installation

Visualization of residential house prices

Nodejs安装

Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise

Go language JSON package usage

C1小笔记【任务训练篇二】

JS high frequency interview questions

SSD硬盘SATA接口和M.2接口区别(详细)总结

7-21 wrong questions involve knowledge points.
随机推荐
Logic regression principle and code implementation
Map basemap Library
列錶的使用-增删改查
Thirteen documents in software engineering
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
Eigen learning summary
Use of list - addition, deletion, modification and query
Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
Auto. JS custom dialog box
Realization of consumer gray scale
Docker 安装 MySQL
Special effects case collection: mouse planet small tail
Fashion classification case based on keras
Qt读写XML文件(含源码+注释)
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
ArcGIS table to excel exceeds the upper limit, conversion failed
ArcGIS license error -15 solution
Stanford machine learning course summary
Visualization of residential house prices