当前位置:网站首页>Rust 中的 RefCell
Rust 中的 RefCell
2022-04-23 05:45:00 【许野平】
RefCell 与 Cell 基本相同,区别在于 RefCell 读取内容时,返回的是引用,本质上是一个指针。这是因为 RefCell 要包装的数据没有实现 Copy 特性。代码示例如下:
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"
rust 的这套工具确实做的很漂亮呀!
版权声明
本文为[许野平]所创,转载请带上原文链接,感谢
https://yeping.blog.csdn.net/article/details/123044634
边栏推荐
- Robocode教程3——Robo机器剖析
- Algèbre linéaire chapitre 2 - matrice et son fonctionnement
- Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
- word排版遇到的格式问题
- [leetcode 350] intersection of two arrays II
- 从源代码到可执行文件的过程
- 斯坦福机器学习课程汇总
- Substring Inversion (Easy Version)
- Kalman filter and inertial integrated navigation
- What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
猜你喜欢
随机推荐
Import of data
卡尔曼滤波与惯性组合导航
JDBC operation transaction
线程和进程的关系和区别是什么
Best practices for MySQL storage time
Type conversion in C #
Collections multiple parameter sorting
Usage scenario of copyonwritearraylist
Definition of C class and method
Generation of verification code
Installation and usage skills of idea
Stability building best practices
Custom exception class
斯坦福机器学习课程汇总
D. Optimal partition segment tree optimization DP
Troubleshooting of data deleted and reappeared problems
队列解决约瑟夫问题
GDAL+OGR学习
Miscellaneous 1
Create binary tree









