-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathref_cell.rs
More file actions
113 lines (95 loc) · 2.58 KB
/
Copy pathref_cell.rs
File metadata and controls
113 lines (95 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![allow(unused)]
use std::cell::{RefCell, RefMut};
use std::rc::Rc;
// Interior mutability
// - Allows data mutatation even when there are immutable references to that data
// - RefCell
// - Interior mutability
// - Run time error when borrowing rules are broken
// - Single threaded use
// - RefCell is used in combination with Rc to create a mutable data with shared ownership
#[derive(Debug)]
enum List {
Cons(i32, Rc<RefCell<List>>),
Nil,
}
#[derive(Debug)]
struct Node {
val: u32,
// Share nodes, mutate the neighbors vector
neighbors: RefCell<Vec<Rc<Node>>>,
}
use crate::List::{Cons, Nil};
fn main() {
// This will not compile
// Cannot borrow mut on immutable value
// let s = String::from("rust");
// let s1 = &mut s;
// s1 += "🦀";
// RefCell
let s = String::from("rust");
let r: RefCell<String> = RefCell::new(s);
{
let mut r1 = r.borrow_mut();
*r1 += "🦀";
// r is borrowed until r1 is dropped
println!("{:#?}", r);
// Run time error - already borrowed
// let mut r2 = r.borrow_mut();
// r1 is dropped
}
// s is owned by r
println!("{:#?}", r);
// Example - update last element
// 1 -> Nil
let a = Rc::new(RefCell::new(Cons(1, Rc::new(RefCell::new(Nil)))));
// 2 -> a
let b = Cons(2, Rc::clone(&a));
// 3 -> a
let c = Cons(3, Rc::clone(&a));
if let Cons(v, _) = &mut *a.borrow_mut() {
*v += 100;
}
println!("a: {a:?}");
println!("b: {b:?}");
println!("c: {c:?}");
// Example - Node
/*
#[derive(Debug)
struct Node<'a> {
pub val: u32,
pub neighbors: Vec<&'a Node<'a>>,
}
let mut node0 = Node {
val: 0,
neighbors: vec![],
};
let mut node1 = Node {
val: 1,
neighbors: vec![],
};
// Does not compile
// node 0 -> node 1 (immutable borrow of node 1)
node0.neighbors.push(&node1);
// node 1 -> node 0 (mutable borrow of node 1)
node1.neighbors.push(&node0);
*/
let node0 = Rc::new(Node {
val: 0,
neighbors: RefCell::new(vec![]),
});
let node1 = Rc::new(Node {
val: 1,
neighbors: RefCell::new(vec![]),
});
{
// node 0 -> node 1
let mut r0: RefMut<'_, Vec<Rc<Node>>> = node0.neighbors.borrow_mut();
r0.push(Rc::clone(&node1));
// node 1 -> node 0
let mut r1: RefMut<'_, Vec<Rc<Node>>> = node1.neighbors.borrow_mut();
r1.push(Rc::clone(&node0));
}
// Infinite loop - panics
// println!("{:?}", node0);
}