当前位置:网站首页>Cells in rust share variable pointers
Cells in rust share variable pointers
2022-04-23 18:03:00 【Xu Yeping】
Rust Medium Cell Shared variable pointer
1 First time to know Cell
Cell and Box It's almost functional , But it allows multiple variables to share content , It is also allowed to modify the content when multiple variables are shared . Let's take an example :
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
println!("{:?}", x);
}
------------------------------------------------------
>cargo run
Cell {
value: 123 }
2 Multiple references can be modified , Modify data in many ways
Be careful , Variable x Not added mut modification , The following code can also be modified . This explanation Cell The internal modification behavior of should adopt some kind of unsafe In the form of , Only from the surface form of the function call, we can't see the behavior of the variable to be modified . In this way, the check of the modification operation is transferred from the compile time to the run time of the , from Cell Your code determines whether there is a modification conflict .
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
x.set(456);
println!("{:?}", x);
}
------------------------------------------------------
>cargo run
Cell {
value: 456 }
Because there is no right x use mut, Therefore, the compiler will think that it is aimed at x All operations are read-only , No errors will be reported during compilation for potential modification actions .
because rust Allow multiple read-only references , therefore ,Cell It's like secretly opening a back door , Allow multiple different references to modify the content of the same variable .
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
let a = &x;
let b = &x;
a.set(456);
b.set(789);
println!("{:?}", x);
}
------------------------------------------------------
>cargo run
Cell {
value: 789 }
in fact , a and b Is directed x Variable references to , But from the surface grammar , It doesn't need to mut keyword , Therefore, you can bypass the syntax check of the compiler .
3 Read the content
If the content book can Copy Of , It can also be used. get Method to copy the contents to other variables .
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
If the content is not implemented Copy characteristic , be get Method cannot be used .
use std::cell::Cell;
fn main() {
let x = Cell::new("hello".to_string());
x.set("nice".to_string());
let y = x.get();
println!("{:?}, {:?}", x, y);
}
------------------------------------------------------
>cargo run
--> src\main.rs:5:15
|
5 | let y = x.get();
| ^^^
|
::: C:\Users\xxxx\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\alloc\src\string.rs:292:1
|
292 | pub struct String {
| ----------------- doesn't satisfy `String: Copy`
版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498565.html
边栏推荐
- 2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
- .105Location
- cv_ Solution of mismatch between bridge and opencv
- Notes on common basic usage of eigen Library
- Data stream encryption and decryption of C
- Array rotation
- Batch export ArcGIS attribute table
- Classification of cifar100 data set based on convolutional neural network
- I / O multiplexing and its related details
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
猜你喜欢
JS high frequency interview questions
开源按键组件Multi_Button的使用,含测试工程
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
Nat commun | current progress and open challenges of applied deep learning in Bioscience
Install pyshp Library
Logic regression principle and code implementation
Random number generation of C #
C network related operations
Halo 开源项目学习(二):实体类与数据表
Visualization of residential house prices
随机推荐
_ FindText error
ArcGIS table to excel exceeds the upper limit, conversion failed
C network related operations
Amount input box, used for recharge and withdrawal
Qtablewidget usage explanation
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
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
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Utilisation de la liste - Ajouter, supprimer et modifier la requête
Romance in C language
k8s之实现redis一主多从动态扩缩容
Summary of floating point double precision, single precision and half precision knowledge
Dock installation redis
Using files to save data (C language)
极致体验,揭晓抖音背后的音视频技术
Flash operates on multiple databases
re正則錶達式
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Calculation of fishing net road density
QTableWidget使用讲解