summaryrefslogtreecommitdiff
path: root/exercises/19_smart_pointers/rc1.rs
diff options
context:
space:
mode:
authormo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
committermo8it <mo8it@proton.me>2024-07-05 13:39:50 +0200
commit7123c7ae3a9605fbe962e4ef0a0f1424cd16fef8 (patch)
treec67f7e62bb9a179ae4fdbab492501cb6847e64c7 /exercises/19_smart_pointers/rc1.rs
parent77b687d501771c24bd83294d97b8e6f9ffa92d6b (diff)
parent4d9c346a173bb722b929f3ea3c00f84954483e24 (diff)
Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency
Diffstat (limited to 'exercises/19_smart_pointers/rc1.rs')
-rw-r--r--exercises/19_smart_pointers/rc1.rs125
1 files changed, 63 insertions, 62 deletions
diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs
index 1b90346..48e19dc 100644
--- a/exercises/19_smart_pointers/rc1.rs
+++ b/exercises/19_smart_pointers/rc1.rs
@@ -1,22 +1,14 @@
-// rc1.rs
-//
// 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.
-//
-// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint.
-
-// I AM NOT DONE
+// `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;
+#[allow(dead_code)]
#[derive(Debug)]
enum Planet {
Mercury(Rc<Sun>),
@@ -31,75 +23,84 @@ enum Planet {
impl Planet {
fn details(&self) {
- println!("Hi from {:?}!", self)
+ println!("Hi from {self:?}!");
}
}
-#[test]
fn main() {
- let sun = Rc::new(Sun {});
- println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
- let mercury = Planet::Mercury(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
- mercury.details();
+ #[test]
+ fn rc1() {
+ let sun = Rc::new(Sun);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
- let venus = Planet::Venus(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
- venus.details();
+ let mercury = Planet::Mercury(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+ mercury.details();
- let earth = Planet::Earth(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
- earth.details();
+ let venus = Planet::Venus(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+ venus.details();
- let mars = Planet::Mars(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
- mars.details();
+ let earth = Planet::Earth(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+ earth.details();
- let jupiter = Planet::Jupiter(Rc::clone(&sun));
- println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
- jupiter.details();
+ let mars = Planet::Mars(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+ mars.details();
- // TODO
- let saturn = Planet::Saturn(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
- saturn.details();
+ let jupiter = Planet::Jupiter(Rc::clone(&sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+ jupiter.details();
- // TODO
- let uranus = Planet::Uranus(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
- uranus.details();
+ // TODO
+ let saturn = Planet::Saturn(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+ saturn.details();
- // TODO
- let neptune = Planet::Neptune(Rc::new(Sun {}));
- println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
- neptune.details();
+ // TODO
+ let uranus = Planet::Uranus(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+ uranus.details();
- assert_eq!(Rc::strong_count(&sun), 9);
+ // TODO
+ let neptune = Planet::Neptune(Rc::new(Sun));
+ println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
+ neptune.details();
- drop(neptune);
- println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
+ assert_eq!(Rc::strong_count(&sun), 9);
- drop(uranus);
- println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
+ drop(neptune);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
- drop(saturn);
- println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
+ drop(uranus);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
- drop(jupiter);
- println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
+ drop(saturn);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
- drop(mars);
- println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
+ drop(jupiter);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
+ drop(mars);
+ println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
- // TODO
- println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
- assert_eq!(Rc::strong_count(&sun), 1);
+ // TODO
+ println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
+
+ assert_eq!(Rc::strong_count(&sun), 1);
+ }
}