|
| 1 | +// rc1.rs |
| 2 | +// In this exercise, we want to express the concept of multiple owners via the Rc<T> type. |
| 3 | +// This is a model of our solar system - there is a Sun type and multiple Planets. |
| 4 | +// The Planets take ownership of the sun, indicating that they revolve around the sun. |
| 5 | + |
| 6 | +// Make this code compile by using the proper Rc primitives to express that the sun has multiple owners. |
| 7 | + |
| 8 | +// I AM NOT DONE |
| 9 | +use std::rc::Rc; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +struct Sun {} |
| 13 | + |
| 14 | +#[derive(Debug)] |
| 15 | +enum Planet { |
| 16 | + Mercury(Rc<Sun>), |
| 17 | + Venus(Rc<Sun>), |
| 18 | + Earth(Rc<Sun>), |
| 19 | + Mars(Rc<Sun>), |
| 20 | + Jupiter(Rc<Sun>), |
| 21 | + Saturn(Rc<Sun>), |
| 22 | + Uranus(Rc<Sun>), |
| 23 | + Neptune(Rc<Sun>), |
| 24 | +} |
| 25 | + |
| 26 | +impl Planet { |
| 27 | + fn details(&self) { |
| 28 | + println!("Hi from {:?}!", self) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +fn main() { |
| 33 | + let sun = Rc::new(Sun {}); |
| 34 | + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference |
| 35 | + |
| 36 | + let mercury = Planet::Mercury(Rc::clone(&sun)); |
| 37 | + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references |
| 38 | + mercury.details(); |
| 39 | + |
| 40 | + let venus = Planet::Venus(Rc::clone(&sun)); |
| 41 | + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references |
| 42 | + venus.details(); |
| 43 | + |
| 44 | + let earth = Planet::Earth(Rc::clone(&sun)); |
| 45 | + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references |
| 46 | + earth.details(); |
| 47 | + |
| 48 | + let mars = Planet::Mars(Rc::clone(&sun)); |
| 49 | + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references |
| 50 | + mars.details(); |
| 51 | + |
| 52 | + let jupiter = Planet::Jupiter(Rc::clone(&sun)); |
| 53 | + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references |
| 54 | + jupiter.details(); |
| 55 | + |
| 56 | + // TODO |
| 57 | + let saturn = Planet::Saturn(Rc::new(Sun {})); |
| 58 | + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references |
| 59 | + saturn.details(); |
| 60 | + |
| 61 | + // TODO |
| 62 | + let uranus = Planet::Uranus(Rc::new(Sun {})); |
| 63 | + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references |
| 64 | + uranus.details(); |
| 65 | + |
| 66 | + // TODO |
| 67 | + let neptune = Planet::Neptune(Rc::new(Sun {})); |
| 68 | + println!("reference count = {}", Rc::strong_count(&sun)); // 9 references |
| 69 | + neptune.details(); |
| 70 | + |
| 71 | + assert_eq!(Rc::strong_count(&sun), 9); |
| 72 | + |
| 73 | + drop(neptune); |
| 74 | + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references |
| 75 | + |
| 76 | + drop(uranus); |
| 77 | + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references |
| 78 | + |
| 79 | + drop(saturn); |
| 80 | + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references |
| 81 | + |
| 82 | + drop(jupiter); |
| 83 | + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references |
| 84 | + |
| 85 | + drop(mars); |
| 86 | + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references |
| 87 | + |
| 88 | + // TODO |
| 89 | + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references |
| 90 | + |
| 91 | + // TODO |
| 92 | + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references |
| 93 | + |
| 94 | + // TODO |
| 95 | + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference |
| 96 | + |
| 97 | + assert_eq!(Rc::strong_count(&sun), 1); |
| 98 | +} |
0 commit comments