diff options
| author | mo8it <mo8it@proton.me> | 2024-06-29 01:20:59 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-06-29 01:20:59 +0200 |
| commit | f3842aa746aa77a3fdf0f699951cde0d49f042c4 (patch) | |
| tree | 60c7293857e72bade7ab6ed4512a87a96a2a026e /exercises/19_smart_pointers | |
| parent | 61c7eaed6251fb8a28b00ea97b22d1f1b778a72b (diff) | |
rc1 solution
Diffstat (limited to 'exercises/19_smart_pointers')
| -rw-r--r-- | exercises/19_smart_pointers/rc1.rs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs index 19de3db..ecd3438 100644 --- a/exercises/19_smart_pointers/rc1.rs +++ b/exercises/19_smart_pointers/rc1.rs @@ -1,15 +1,12 @@ // In this exercise, we want to express the concept of multiple owners via the -// Rc<T> type. This is a model of our solar system - there is a Sun type and -// multiple Planets. The Planets take ownership of the sun, indicating that they -// revolve around the sun. -// -// Make this code compile by using the proper Rc primitives to express that the -// sun has multiple owners. +// `Rc<T>` type. This is a model of our solar system - there is a `Sun` type and +// multiple `Planet`s. The planets take ownership of the sun, indicating that +// they revolve around the sun. use std::rc::Rc; #[derive(Debug)] -struct Sun {} +struct Sun; #[derive(Debug)] enum Planet { @@ -25,7 +22,7 @@ enum Planet { impl Planet { fn details(&self) { - println!("Hi from {:?}!", self) + println!("Hi from {self:?}!"); } } @@ -39,7 +36,7 @@ mod tests { #[test] fn rc1() { - let sun = Rc::new(Sun {}); + let sun = Rc::new(Sun); println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference let mercury = Planet::Mercury(Rc::clone(&sun)); @@ -63,17 +60,17 @@ mod tests { jupiter.details(); // TODO - let saturn = Planet::Saturn(Rc::new(Sun {})); + let saturn = Planet::Saturn(Rc::new(Sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 7 references saturn.details(); // TODO - let uranus = Planet::Uranus(Rc::new(Sun {})); + let uranus = Planet::Uranus(Rc::new(Sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 8 references uranus.details(); // TODO - let neptune = Planet::Neptune(Rc::new(Sun {})); + let neptune = Planet::Neptune(Rc::new(Sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 9 references neptune.details(); |
