当前位置:网站首页>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
边栏推荐
- Substring Inversion (Easy Version)
- Cf515b drazil and his happy friends
- [leetcode 459] duplicate substring
- 从源代码到可执行文件的过程
- 进程间通信的方式
- 11.a==b?
- How SYSTEMd uses / etc / init D script
- Protected (members modified by protected are visible to this package and its subclasses)
- 5.The Simple Problem
- Addition, deletion, query and modification of data
猜你喜欢
Best practices for MySQL storage time
Why does the subscript of the array start from 0 instead of 1?
Cf1427c the hard work of paparazzi
MySQL advanced query
Storing inherited knowledge in cloud computing
Installation and usage skills of idea
几行代码教你爬取LOL皮肤图片
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
Mysql database foundation
Export of data
随机推荐
Rust 中的 Cell 共享可变指针
4. Print form
Class loading and classloader understanding
Advanced operation of idea debug
Explain of MySQL optimization
破解滑动验证码
Fact final variable and final variable
Cf515b drazil and his happy friends
[leetcode 228] summary interval
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
[untitled] database - limit the number of returned rows
C array
How does MySQL convert stored seconds into dates
MySQL advanced query
Understanding and use of tp50, tp90 and tp99
Problems and solutions of database migration
Storing inherited knowledge in cloud computing
GNU EFI header file
进程间通信的方式
RPC must know and know