Going through rustlings for the first time
at main 104 lines 3.1 kB view raw
1// In this exercise, we want to express the concept of multiple owners via the 2// `Rc<T>` type. This is a model of our solar system - there is a `Sun` type and 3// multiple `Planet`s. The planets take ownership of the sun, indicating that 4// they revolve around the sun. 5 6use std::rc::Rc; 7 8#[derive(Debug)] 9struct Sun; 10 11#[derive(Debug)] 12enum Planet { 13 Mercury(Rc<Sun>), 14 Venus(Rc<Sun>), 15 Earth(Rc<Sun>), 16 Mars(Rc<Sun>), 17 Jupiter(Rc<Sun>), 18 Saturn(Rc<Sun>), 19 Uranus(Rc<Sun>), 20 Neptune(Rc<Sun>), 21} 22 23impl Planet { 24 fn details(&self) { 25 println!("Hi from {self:?}!"); 26 } 27} 28 29fn main() { 30 // You can optionally experiment here. 31} 32 33#[cfg(test)] 34mod tests { 35 use super::*; 36 37 #[test] 38 fn rc1() { 39 let sun = Rc::new(Sun); 40 println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference 41 42 let mercury = Planet::Mercury(Rc::clone(&sun)); 43 println!("reference count = {}", Rc::strong_count(&sun)); // 2 references 44 mercury.details(); 45 46 let venus = Planet::Venus(Rc::clone(&sun)); 47 println!("reference count = {}", Rc::strong_count(&sun)); // 3 references 48 venus.details(); 49 50 let earth = Planet::Earth(Rc::clone(&sun)); 51 println!("reference count = {}", Rc::strong_count(&sun)); // 4 references 52 earth.details(); 53 54 let mars = Planet::Mars(Rc::clone(&sun)); 55 println!("reference count = {}", Rc::strong_count(&sun)); // 5 references 56 mars.details(); 57 58 let jupiter = Planet::Jupiter(Rc::clone(&sun)); 59 println!("reference count = {}", Rc::strong_count(&sun)); // 6 references 60 jupiter.details(); 61 62 let saturn = Planet::Saturn(Rc::clone(&sun)); 63 println!("reference count = {}", Rc::strong_count(&sun)); // 7 references 64 saturn.details(); 65 66 // TODO 67 let uranus = Planet::Uranus(Rc::clone(&sun)); 68 println!("reference count = {}", Rc::strong_count(&sun)); // 8 references 69 uranus.details(); 70 71 // TODO 72 let neptune = Planet::Neptune(Rc::clone(&sun)); 73 println!("reference count = {}", Rc::strong_count(&sun)); // 9 references 74 neptune.details(); 75 76 assert_eq!(Rc::strong_count(&sun), 9); 77 78 drop(neptune); 79 println!("reference count = {}", Rc::strong_count(&sun)); // 8 references 80 81 drop(uranus); 82 println!("reference count = {}", Rc::strong_count(&sun)); // 7 references 83 84 drop(saturn); 85 println!("reference count = {}", Rc::strong_count(&sun)); // 6 references 86 87 drop(jupiter); 88 println!("reference count = {}", Rc::strong_count(&sun)); // 5 references 89 90 drop(mars); 91 println!("reference count = {}", Rc::strong_count(&sun)); // 4 references 92 93 drop(earth); 94 println!("reference count = {}", Rc::strong_count(&sun)); // 3 references 95 96 drop(venus); 97 println!("reference count = {}", Rc::strong_count(&sun)); // 2 references 98 99 drop(mercury); 100 println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference 101 102 assert_eq!(Rc::strong_count(&sun), 1); 103 } 104}