-
In the bin folder, create a file called shared.rs
-
Add the following code and run it with cargo run --bin shared:
1 use std::rc::Rc;
2
3 // The ball will survive until all kids are done playing with
it
4 struct Kid {
5 ball: Rc<Ball>,
6 }
7 struct Ball;
8
9 fn main() {
10 {
11 // rc is created and count is at 1
12 let foo = Rc::new("foo");
13 // foo goes out of scope; count decreases
14 // count is zero; the object gets destroyed
15 }
16
17 {
18 // rc is created and count is at 1
19 let bar = Rc::new("bar");
20 // rc is cloned; count increases to 2
21 let second_bar = Rc::clone(&bar);
22 // bar goes out of scode; count decreases to 1
23 // bar goes out of scode; count decreases to 0
24 }
25
26 {
27 // rc is created and count is at 1
28 let baz = Rc::new("baz");
29 {
30 // rc is cloned; count increases to...