当前位置:网站首页>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
边栏推荐
- Data stream encryption and decryption of C
- Implementation of image recognition code based on VGg convolutional neural network
- Notes on common basic usage of eigen Library
- .104History
- [UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
- undefined reference to `Nabo::NearestNeighbourSearch
- Flash operates on multiple databases
- Basic usage of crawler requests
- Random number generation of C #
- 解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
猜你喜欢
Nat commun | current progress and open challenges of applied deep learning in Bioscience
Qt读写XML文件(含源码+注释)
Remember using Ali Font Icon Library for the first time
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
Gaode map search, drag and drop query address
由tcl脚本生成板子对应的vivado工程
Docker 安装 Redis
Using files to save data (C language)
Batch export ArcGIS attribute table
Halo open source project learning (II): entity classes and data tables
随机推荐
I/O多路复用及其相关详解
Climbing watermelon video URL
Excel opens large CSV format data
Eigen learning summary
Svn simple operation command
列錶的使用-增删改查
Using files to save data (C language)
Secure credit
Data stream encryption and decryption of C
C# 的数据流加密与解密
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
Timestamp to formatted date
C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
Docker installation MySQL
Generate verification code
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
Tensorflow tensor introduction
Pyppeter crawler
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
JS high frequency interview questions