当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
[leetcode 350] intersection of two arrays II
Three ways to create threads
7.Domino piling
Usage scenario of copyonwritearraylist
Generation of verification code
Rainbow (DP)
Failure to deliver XID in Seata distributed transaction project
程序设计训练
Plane semi intersecting plate
Framework analysis 2 Source code - login authentication
[leetcode 54] spiral matrix
IO multiplexing of 09 redis
C array
[leetcode169] most elements
1. Calculate a + B
Code neat way to learn
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
Exception handling: grab and throw model
Example of reentrant lock thread waiting to wake up
SQL optimization best practices









