Skip to content

Commit 8a4ed70

Browse files
authored
Merge pull request #1069 from exdx/rc-exercise
feat: add rc1.rs exercise
2 parents 25ab52b + 1c3b003 commit 8a4ed70

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// rc1.rs
2+
// In this exercise, we want to express the concept of multiple owners via the Rc<T> type.
3+
// This is a model of our solar system - there is a Sun type and multiple Planets.
4+
// The Planets take ownership of the sun, indicating that they revolve around the sun.
5+
6+
// Make this code compile by using the proper Rc primitives to express that the sun has multiple owners.
7+
8+
// I AM NOT DONE
9+
use std::rc::Rc;
10+
11+
#[derive(Debug)]
12+
struct Sun {}
13+
14+
#[derive(Debug)]
15+
enum Planet {
16+
Mercury(Rc<Sun>),
17+
Venus(Rc<Sun>),
18+
Earth(Rc<Sun>),
19+
Mars(Rc<Sun>),
20+
Jupiter(Rc<Sun>),
21+
Saturn(Rc<Sun>),
22+
Uranus(Rc<Sun>),
23+
Neptune(Rc<Sun>),
24+
}
25+
26+
impl Planet {
27+
fn details(&self) {
28+
println!("Hi from {:?}!", self)
29+
}
30+
}
31+
32+
fn main() {
33+
let sun = Rc::new(Sun {});
34+
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
35+
36+
let mercury = Planet::Mercury(Rc::clone(&sun));
37+
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
38+
mercury.details();
39+
40+
let venus = Planet::Venus(Rc::clone(&sun));
41+
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
42+
venus.details();
43+
44+
let earth = Planet::Earth(Rc::clone(&sun));
45+
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
46+
earth.details();
47+
48+
let mars = Planet::Mars(Rc::clone(&sun));
49+
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
50+
mars.details();
51+
52+
let jupiter = Planet::Jupiter(Rc::clone(&sun));
53+
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
54+
jupiter.details();
55+
56+
// TODO
57+
let saturn = Planet::Saturn(Rc::new(Sun {}));
58+
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
59+
saturn.details();
60+
61+
// TODO
62+
let uranus = Planet::Uranus(Rc::new(Sun {}));
63+
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
64+
uranus.details();
65+
66+
// TODO
67+
let neptune = Planet::Neptune(Rc::new(Sun {}));
68+
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
69+
neptune.details();
70+
71+
assert_eq!(Rc::strong_count(&sun), 9);
72+
73+
drop(neptune);
74+
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
75+
76+
drop(uranus);
77+
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
78+
79+
drop(saturn);
80+
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
81+
82+
drop(jupiter);
83+
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
84+
85+
drop(mars);
86+
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
87+
88+
// TODO
89+
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
90+
91+
// TODO
92+
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
93+
94+
// TODO
95+
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
96+
97+
assert_eq!(Rc::strong_count(&sun), 1);
98+
}

info.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,20 @@ is too much of a struggle, consider reading through all of Chapter 16 in the boo
932932
https://doc.rust-lang.org/stable/book/ch16-00-concurrency.html
933933
"""
934934

935+
[[exercises]]
936+
name = "rc1"
937+
path = "exercises/standard_library_types/rc1.rs"
938+
mode = "compile"
939+
hint = """
940+
This is a straightforward exercise to use the Rc<T> type. Each Planet has
941+
ownership of the Sun, and uses Rc::clone() to increment the reference count of the Sun.
942+
After using drop() to move the Planets out of scope individually, the reference count goes down.
943+
In the end the sun only has one reference again, to itself. See more at:
944+
https://doc.rust-lang.org/book/ch15-04-rc.html
945+
946+
* Unforunately Pluto is no longer considered a planet :(
947+
"""
948+
935949
[[exercises]]
936950
name = "cow1"
937951
path = "exercises/standard_library_types/cow1.rs"

0 commit comments

Comments
 (0)