当前位置:网站首页>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
边栏推荐
猜你喜欢

Implementation of image recognition code based on VGg convolutional neural network

Go的Gin框架学习
![解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]](/img/5f/a80951777a0473fcaa685cd6a8e5dd.png)
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战

Batch export ArcGIS attribute table

C#的随机数生成

cv_ Solution of mismatch between bridge and opencv

re正則錶達式
![[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)

JS get link? The following parameter name or value, according to the URL? Judge the parameters after
随机推荐
Go's gin framework learning
SSD硬盘SATA接口和M.2接口区别(详细)总结
Qt读写XML文件(含源码+注释)
C#字节数组(byte[])和字符串相互转换
C language input and output (printf and scanf functions, putchar and getchar functions)
Fashion classification case based on keras
word frequency count
Halo open source project learning (II): entity classes and data tables
Build openstack platform
Crawl the product data of Xiaomi Youpin app
Queue solving Joseph problem
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Arcpy adds fields and loop assignments to vector data
587. Install fence / Sword finger offer II 014 Anagrams in strings
Identification verification code
消费者灰度实现思路
Thirteen documents in software engineering
Nodejs安装
Summary of common server error codes