rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

rc1.rs (3113B)


      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 
      6 use std::rc::Rc;
      7 
      8 #[derive(Debug)]
      9 struct Sun;
     10 
     11 #[derive(Debug)]
     12 enum 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 
     23 impl Planet {
     24     fn details(&self) {
     25         println!("Hi from {self:?}!");
     26     }
     27 }
     28 
     29 fn main() {
     30     // You can optionally experiment here.
     31 }
     32 
     33 #[cfg(test)]
     34 mod 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         // TODO
     63         let saturn = Planet::Saturn(Rc::new(Sun));
     64         println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
     65         saturn.details();
     66 
     67         // TODO
     68         let uranus = Planet::Uranus(Rc::new(Sun));
     69         println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
     70         uranus.details();
     71 
     72         // TODO
     73         let neptune = Planet::Neptune(Rc::new(Sun));
     74         println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
     75         neptune.details();
     76 
     77         assert_eq!(Rc::strong_count(&sun), 9);
     78 
     79         drop(neptune);
     80         println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
     81 
     82         drop(uranus);
     83         println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
     84 
     85         drop(saturn);
     86         println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
     87 
     88         drop(jupiter);
     89         println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
     90 
     91         drop(mars);
     92         println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
     93 
     94         // TODO
     95         println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
     96 
     97         // TODO
     98         println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
     99 
    100         // TODO
    101         println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
    102 
    103         assert_eq!(Rc::strong_count(&sun), 1);
    104     }
    105 }