当前位置:网站首页>Pointers in rust: box, RC, cell, refcell
Pointers in rust: box, RC, cell, refcell
2022-04-23 18:03:00 【Xu Yeping】
Rust The pointer in :Box、Rc、Cell、RefCell
author : Xu Yeping 2022-02-21
Rust It has its own &、* The operator , Variable reference and dereference can be realized . Why come up with these types of pointers ? The reason is to break through Rust Persistent ” Sharing is not writable 、 Writable but not shared “ Principles . Let's see. Rust Compiler ” Moral bottom line “ How to break through step by step .
1 Box type
I am here 《 Rust Of Box The pointer 》 This paper discusses in detail Box Characteristics . Take a simple example :
fn main() {
let x = String::from("Hello!");
let y = Box::new(x);
println!("{:?}", y);
}
In fact, this code is almost equivalent to the following code :
fn main() {
let x = String::from("Hello!");
let y = &x;
println!("{:?}", y);
}
There's another paragraph Box Code , Shows the usage when designing the linked list structure :
#[derive (Debug)]
struct Node {
data: i32,
next: Option<Box<Node>>,
}
fn main() {
let x = Node {
data: 123,
next: None,
};
let y = Box::new(x);
println!("{:?}", y);
}
---------------------------------
>cargo run
Node {
data: 123, next: None }
I try to put Box Change to &, As a result, because of the problem of life cycle , I haven't been able to compile for a long time . Paste the code below , Which great God can give me some ?
#[derive (Debug)]
struct Node <'a>{
data: i32,
next: Option<&'a Node>,
}
fn main() {
let x = Node {
data: 123,
next: None,
};
let y = Box::new(x);
println!("{:?}", y);
}
---------------------------------
>cargo run
--> src\main.rs:4:22
|
4 | next: Option<&'a Node>,
| ^^^^ expected named lifetime parameter
|
help: consider using the `'a` lifetime
Never mind & Can you replace it Box, Anyway, tell me about it ,Box Helped us simplify a lot of work , It's better than using it directly & It's easier .
2 Rc type
Look at the code below :
use std::rc::Rc;
fn main() {
let x = Rc::new(123);
let y = x.clone();
println!("{:?}, {:?}", x, y);
}
---------------------------------------------------
cargo run
123, 123
In fact, logically speaking , It can also be used. & Instead of . The address of a variable can be assigned to multiple variables , Isn't it ? But rust Life cycle problems in , Enough for us to drink a pot . therefore ,Rc The existence value of is to avoid life cycle inspection , So that the same data can be referenced in multiple places .
3 Cell type
Cell Types are covered with read-only variables , Allow the program to modify the contents of variables . Even though Rust There is a principle ——“ Sharing is not writable , Writable but not shared ”, because Cell Seemingly read-only , therefore ,Cell Type of data can be referenced in multiple places , Realize that the same data can be modified by multiple shared references . because Cell Grammatically, it is read-only , So the compiler doesn't report errors .
Cell It can be used get Method returns data . Because of the execution of Copy Method , Therefore, it is required that the data must realize Copy characteristic .
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
x.set(456);
let y = x.get();
println!("{:?}, {:?}", x, y);
}
------------------------------------------------------
>cargo run
Cell {
value: 456 }, 456
4 RefCell
RefCell And Cell Basically the same , The difference lies in RefCell When reading content , The return is the reference , It's essentially a pointer . This is because RefCell The data to be packaged is not implemented Copy characteristic . The code example is as follows :
use std::cell::{
Ref, RefCell};
fn main() {
let x = RefCell::new("good".to_string());
let a = &x;
let b = &x;
*a.borrow_mut() = "nice".to_string();
*b.borrow_mut() = "best".to_string();
let y: Ref<String> = x.borrow();
println!("x = {:?}", x);
println!("y = {:?}", y);
}
---------------------------------------------------
>cargo run
x = RefCell {
value: "best" }
y = "best"
5 summary
- Box And so on , Simplifies the variable life cycle problem .
- Rc allow clone() Method to produce multiple copies of the variable , But these copies don't really allocate memory , But shared the same data .
- Cell Syntactically, it is a read-only reference , In fact, it can be modified .Cell In principle, you can only refer to the implementation Copy Characteristic variables .
- RefCell And Cell similar , But you can refer to unimplemented Copy Characteristic variables .
版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498483.html
边栏推荐
- Auto.js 自定义对话框
- Jenkspy package installation
- Cloud native Virtualization: building edge computing instances based on kubevirt
- Climbing watermelon video URL
- Encapsulate a timestamp to date method on string prototype
- String function in MySQL
- How to read literature
- C1小笔记【任务训练篇一】
- Operators in C language
- _ FindText error
猜你喜欢

Docker 安装 Redis

Nodejs安装

Docker 安裝 Redis

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

Clion installation tutorial

Fashion classification case based on keras

Cloud native Virtualization: building edge computing instances based on kubevirt
![[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)

Installation du docker redis

.105Location
随机推荐
Cross domain settings of Chrome browser -- including new and old versions
Docker 安装 Redis
Timestamp to formatted date
Jenkspy package installation
Qt读写XML文件(含源码+注释)
Identification verification code
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
C#的随机数生成
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
The ultimate experience, the audio and video technology behind the tiktok
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
Implementation of object detection case based on SSD
I / O multiplexing and its related details
Utilisation de la liste - Ajouter, supprimer et modifier la requête
20222 return to the workplace
Install pyshp Library
Re expression régulière
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing