rustlings/exercises/19_smart_pointers/rc1.rs

107 lines
3.1 KiB
Rust
Raw Normal View History

// In this exercise, we want to express the concept of multiple owners via the
2024-06-29 00:20:59 +01:00
// `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.
2022-07-16 20:31:37 +01:00
use std::rc::Rc;
#[derive(Debug)]
2024-06-29 00:20:59 +01:00
struct Sun;
2022-07-16 20:31:37 +01:00
2024-07-04 12:38:35 +01:00
#[allow(dead_code)]
2022-07-16 20:31:37 +01:00
#[derive(Debug)]
enum Planet {
Mercury(Rc<Sun>),
Venus(Rc<Sun>),
Earth(Rc<Sun>),
Mars(Rc<Sun>),
Jupiter(Rc<Sun>),
Saturn(Rc<Sun>),
Uranus(Rc<Sun>),
Neptune(Rc<Sun>),
}
impl Planet {
fn details(&self) {
2024-06-29 00:20:59 +01:00
println!("Hi from {self:?}!");
2022-07-16 20:31:37 +01:00
}
}
fn main() {
2024-04-17 22:34:27 +01:00
// You can optionally experiment here.
}
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
#[cfg(test)]
mod tests {
use super::*;
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
#[test]
fn rc1() {
2024-06-29 00:20:59 +01:00
let sun = Rc::new(Sun);
2024-04-17 22:34:27 +01:00
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
let mercury = Planet::Mercury(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
mercury.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
let venus = Planet::Venus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
venus.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
let earth = Planet::Earth(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
earth.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
let mars = Planet::Mars(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
mars.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
let jupiter = Planet::Jupiter(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
jupiter.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
// TODO
2024-06-29 00:20:59 +01:00
let saturn = Planet::Saturn(Rc::new(Sun));
2024-04-17 22:34:27 +01:00
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
// TODO
2024-06-29 00:20:59 +01:00
let uranus = Planet::Uranus(Rc::new(Sun));
2024-04-17 22:34:27 +01:00
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
// TODO
2024-06-29 00:20:59 +01:00
let neptune = Planet::Neptune(Rc::new(Sun));
2024-04-17 22:34:27 +01:00
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details();
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
assert_eq!(Rc::strong_count(&sun), 9);
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
drop(neptune);
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
drop(uranus);
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
drop(saturn);
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
drop(jupiter);
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
drop(mars);
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
// TODO
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
2022-07-16 20:31:37 +01:00
2024-04-17 22:34:27 +01:00
// TODO
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1);
}
2022-07-16 20:31:37 +01:00
}