当前位置:网站首页>Rust 中的 Cell 共享可变指针
Rust 中的 Cell 共享可变指针
2022-04-23 05:45:00 【许野平】
Rust 中的 Cell 共享可变指针
1 初识 Cell
Cell 和 Box 功能差不多,但是它既允许多个变量共享内容,也允许在多个变量共享状态下修改内容。先看个例子:
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
println!("{:?}", x);
}
------------------------------------------------------
>cargo run
Cell {
value: 123 }
2 多个可修改的引用,从多种途径修改数据
注意,变量 x 并未加 mut 修饰,下面的代码一样可以对其修改。这说明 Cell 的内部修改行为应该是采用了某种 unsafe 的形式进行的,仅从函数调用的表面形式上看不出要修改变量的行为。这样就把修改操作的检查从编译期转移到的运行期,由 Cell 的代码确定是否存在修改的冲突。
use std::cell::Cell;
fn main() {
let x = Cell::new(123);
x.set(456);
println!("{:?}", x);
}
------------------------------------------------------
>cargo run
Cell {
value: 456 }
由于没有对 x 用 mut,因此编译器会认为针对 x 的操作都是只读的,不会对潜在的修改动作在编译期间报错。
由于 rust 允许多个只读的引用,因此,Cell 等于偷偷开了个后门,允许多个不同的引用修改同一个变量内容。
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 }
事实上, a 和 b 都是指向 x 的可变引用,但是从表面语法上看,并不需要 mut 关键字,因此可以绕过编译器的语法检查。
3 读取内容
如果内容书可以 Copy 的,也可以用 get 方法把内容复制到其他变量。
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
如果内容未实现 Copy 特性,则 get 方法不能使用。
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`
版权声明
本文为[许野平]所创,转载请带上原文链接,感谢
https://yeping.blog.csdn.net/article/details/123043823
边栏推荐
- Detection technology and principle
- Framework analysis 1 Introduction to system architecture
- 6.Reversal
- LockSupport. Park and unpark, wait and notify
- 3. Continuous integer
- H. Are You Safe? Convex hull naked problem
- Explain of MySQL optimization
- [leetcode 383] ransom letter
- [leetcode 59] spiral matrix II
- 線性代數第二章-矩陣及其運算
猜你喜欢
Filebrowser realizes private network disk
Chapter 4 of line generation - linear correlation of vector systems
C language file operation
List segmentation best practices
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
[leetcode 67] sum of two binary numbers
线性代数第一章-行列式
Generation of verification code
Practical operation - Nacos installation and configuration
In depth source code analysis servlet first program
随机推荐
Automatic control (Han min version)
20 excellent plug-ins recommended by idea
D. Optimal partition segment tree optimization DP
Code neat way to learn
电机与拖动(戚金清版)学习整理
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Numpy common function table sorting of data processing
LockSupport. Park and unpark, wait and notify
[leetcode 59] spiral matrix II
Addition, deletion, modification and query of MySQL table
-- SQL query and return limit rows
Formation à la programmation
PyTorch笔记——实现线性回归完整代码&手动或自动计算梯度代码对比
9.Life, the Universe, and Everything
Collections multiple parameter sorting
Problems and solutions of database migration
How to grow at work
[leetcode 383] ransom letter
Export of data
Example of reentrant lock thread waiting to wake up