Skip to content

Commit 3e654ab

Browse files
committed
// 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 use std::rc::Rc; #[derive(Debug)] struct Sun {} #[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) { println!("Hi from {:?}!", self) } } fn main() { let sun = Rc::new(Sun {}); println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference let mercury = Planet::Mercury(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 2 references mercury.details(); let venus = Planet::Venus(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 3 references venus.details(); let earth = Planet::Earth(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 4 references earth.details(); let mars = Planet::Mars(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 5 references mars.details(); let jupiter = Planet::Jupiter(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 6 references jupiter.details(); // 使用 Rc::clone 共享同一个 sun,而非创建新的 Sun let saturn = Planet::Saturn(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 7 references saturn.details(); // 使用 Rc::clone 共享同一个 sun let uranus = Planet::Uranus(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 8 references uranus.details(); // 使用 Rc::clone 共享同一个 sun let neptune = Planet::Neptune(Rc::clone(&sun)); println!("reference count = {}", Rc::strong_count(&sun)); // 9 references neptune.details(); assert_eq!(Rc::strong_count(&sun), 9); drop(neptune); println!("reference count = {}", Rc::strong_count(&sun)); // 8 references drop(uranus); println!("reference count = {}", Rc::strong_count(&sun)); // 7 references drop(saturn); println!("reference count = {}", Rc::strong_count(&sun)); // 6 references drop(jupiter); println!("reference count = {}", Rc::strong_count(&sun)); // 5 references drop(mars); println!("reference count = {}", Rc::strong_count(&sun)); // 4 references // 释放 earth,引用计数减为 3 drop(earth); println!("reference count = {}", Rc::strong_count(&sun)); // 3 references // 释放 venus,引用计数减为 2 drop(venus); println!("reference count = {}", Rc::strong_count(&sun)); // 2 references // 释放 mercury,引用计数减为 1 drop(mercury); println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference assert_eq!(Rc::strong_count(&sun), 1); }
1 parent 0eea69f commit 3e654ab

File tree

1 file changed

+12
-9
lines changed
  • exercises/smart_pointers

1 file changed

+12
-9
lines changed

exercises/smart_pointers/rc1.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ fn main() {
5959
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
6060
jupiter.details();
6161

62-
// TODO
63-
let saturn = Planet::Saturn(Rc::new(Sun {}));
62+
// 使用 Rc::clone 共享同一个 sun,而非创建新的 Sun
63+
let saturn = Planet::Saturn(Rc::clone(&sun));
6464
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
6565
saturn.details();
6666

67-
// TODO
68-
let uranus = Planet::Uranus(Rc::new(Sun {}));
67+
// 使用 Rc::clone 共享同一个 sun
68+
let uranus = Planet::Uranus(Rc::clone(&sun));
6969
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
7070
uranus.details();
7171

72-
// TODO
73-
let neptune = Planet::Neptune(Rc::new(Sun {}));
72+
// 使用 Rc::clone 共享同一个 sun
73+
let neptune = Planet::Neptune(Rc::clone(&sun));
7474
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
7575
neptune.details();
7676

@@ -91,13 +91,16 @@ fn main() {
9191
drop(mars);
9292
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
9393

94-
// TODO
94+
// 释放 earth,引用计数减为 3
95+
drop(earth);
9596
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
9697

97-
// TODO
98+
// 释放 venus,引用计数减为 2
99+
drop(venus);
98100
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
99101

100-
// TODO
102+
// 释放 mercury,引用计数减为 1
103+
drop(mercury);
101104
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
102105

103106
assert_eq!(Rc::strong_count(&sun), 1);

0 commit comments

Comments
 (0)