当前位置:网站首页>Refcell in rust

Refcell in rust

2022-04-23 18:03:00 Xu Yeping

RefCell And Cell Basically the same , The difference lies in RefCell When reading content , The return is the reference , It's essentially a pointer . This is because RefCell The data to be packaged is not implemented Copy characteristic . The code example is as follows :

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 This set of tools is really beautiful !

版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498524.html