Skip to content

Commit 769ac9e

Browse files
More exercises
1 parent 6c8d651 commit 769ac9e

File tree

6 files changed

+226
-137
lines changed

6 files changed

+226
-137
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
=================
2-
Exercise: ROT13
3-
=================
4-
5-
-----------------
6-
ROT13 Problem
7-
-----------------
8-
9-
In this example, you will implement the classic
10-
:url:`"ROT13" cipher <https://en.wikipedia.org/wiki/ROT13>`. Copy this code to the
11-
playground, and implement the missing bits. Only rotate ASCII alphabetic
12-
characters, to ensure the result is still valid UTF-8.
13-
14-
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-head :end-before://ANCHOR-solution :code:rust :number-lines:1
15-
16-
// Implement the `Read` trait for `RotDecoder`.
17-
-----------------
18-
ROT13 Main
19-
-----------------
20-
21-
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-main :code:rust :number-lines:1
22-
23-
-----------------
24-
ROT13 Solution
25-
-----------------
26-
27-
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-solution :end-before://ANCHOR-main :code:rust :number-lines:1
28-
29-
What happens if you chain two :rust:`RotDecoder` instances together, each
30-
rotating by 13 characters?
1+
=================
2+
Exercise: ROT13
3+
=================
4+
5+
-----------------
6+
ROT13 Problem
7+
-----------------
8+
9+
In this example, you will implement the classic
10+
:url:`"ROT13" cipher <https://en.wikipedia.org/wiki/ROT13>`. Copy this code to the
11+
playground, and implement the missing bits. Only rotate ASCII alphabetic
12+
characters, to ensure the result is still valid UTF-8.
13+
14+
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-head :end-before://ANCHOR-solution :code:rust :number-lines:1
15+
16+
// Implement the `Read` trait for `RotDecoder`.
17+
-----------------
18+
ROT13 Main
19+
-----------------
20+
21+
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-main :code:rust :number-lines:1
22+
23+
-----------------
24+
ROT13 Solution
25+
-----------------
26+
27+
.. container:: source_include 120_std_traits/src/120_std_traits.rs :start-after://ANCHOR-solution :end-before://ANCHOR-main :code:rust :number-lines:1
28+
29+
What happens if you chain two :rust:`RotDecoder` instances together, each
30+
rotating by 13 characters?
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,78 @@
1-
========================
2-
Exercise: Builder Type
3-
========================
4-
5-
------------------------
6-
Builder Type Setup
7-
------------------------
8-
9-
In this example, we will implement a complex data type that owns all of
10-
its data. We will use the *builder pattern* to support building a new
11-
value piece-by-piece, using convenience functions.
12-
13-
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package :end-before://ANCHOR-package_solution :code:rust
14-
15-
.. code:: Rust
16-
17-
impl Package {
18-
/// Return a representation of this package as a dependency, for use in
19-
/// building other packages.
20-
fn as_dependency(&self) -> Dependency {
21-
todo!("1")
22-
}
23-
}
24-
25-
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder :end-before://ANCHOR-builder_solution :code:rust
26-
27-
------------------------
28-
Builder Type Problem
29-
------------------------
30-
31-
Fill in the missing pieces.
32-
33-
.. code:: rust
34-
35-
impl PackageBuilder {
36-
fn new(name: impl Into<String>) -> Self {
37-
todo!("2")
38-
}
39-
40-
/// Set the package version.
41-
fn version(mut self, version: impl Into<String>) -> Self {
42-
self.0.version = version.into();
43-
self
44-
}
45-
46-
/// Set the package authors.
47-
fn authors(mut self, authors: Vec<String>) -> Self {
48-
todo!("3")
49-
}
50-
51-
/// Add an additional dependency.
52-
fn dependency(mut self, dependency: Dependency) -> Self {
53-
todo!("4")
54-
}
55-
56-
/// Set the language. If not set, language defaults to None.
57-
fn language(mut self, language: Language) -> Self {
58-
todo!("5")
59-
}
60-
61-
fn build(self) -> Package {
62-
self.0
63-
}
64-
}
65-
66-
------------------------
67-
Builder Type Main
68-
------------------------
69-
70-
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-main :code:rust
71-
72-
------------------------
73-
Builder Type Solution
74-
------------------------
75-
76-
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package_solution :end-before://ANCHOR-builder :code:rust
77-
78-
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder_solution :end-before://ANCHOR-main :code:rust
1+
========================
2+
Exercise: Builder Type
3+
========================
4+
5+
------------------------
6+
Builder Type Setup
7+
------------------------
8+
9+
In this example, we will implement a complex data type that owns all of
10+
its data. We will use the *builder pattern* to support building a new
11+
value piece-by-piece, using convenience functions.
12+
13+
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package :end-before://ANCHOR-package_solution :code:rust
14+
15+
.. code:: Rust
16+
17+
impl Package {
18+
/// Return a representation of this package as a dependency, for use in
19+
/// building other packages.
20+
fn as_dependency(&self) -> Dependency {
21+
todo!("1")
22+
}
23+
}
24+
25+
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder :end-before://ANCHOR-builder_solution :code:rust
26+
27+
------------------------
28+
Builder Type Problem
29+
------------------------
30+
31+
Fill in the missing pieces.
32+
33+
.. code:: rust
34+
35+
impl PackageBuilder {
36+
fn new(name: impl Into<String>) -> Self {
37+
todo!("2")
38+
}
39+
40+
/// Set the package version.
41+
fn version(mut self, version: impl Into<String>) -> Self {
42+
self.0.version = version.into();
43+
self
44+
}
45+
46+
/// Set the package authors.
47+
fn authors(mut self, authors: Vec<String>) -> Self {
48+
todo!("3")
49+
}
50+
51+
/// Add an additional dependency.
52+
fn dependency(mut self, dependency: Dependency) -> Self {
53+
todo!("4")
54+
}
55+
56+
/// Set the language. If not set, language defaults to None.
57+
fn language(mut self, language: Language) -> Self {
58+
todo!("5")
59+
}
60+
61+
fn build(self) -> Package {
62+
self.0
63+
}
64+
}
65+
66+
------------------------
67+
Builder Type Main
68+
------------------------
69+
70+
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-main :code:rust
71+
72+
------------------------
73+
Builder Type Solution
74+
------------------------
75+
76+
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-package_solution :end-before://ANCHOR-builder :code:rust
77+
78+
.. container:: source_include 130_memory_management/src/130_memory_management.rs :start-after://ANCHOR-builder_solution :end-before://ANCHOR-main :code:rust

courses/comprehensive_rust_training/150_borrowing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ Borrowing
4040
.. include:: 150_borrowing/02_borrowck.rst
4141
.. include:: 150_borrowing/03_examples.rst
4242
.. include:: 150_borrowing/04_interior_mutability.rst
43-
.. include:: 150_borrowing/05_exercise.rst
43+
.. include:: 150_borrowing/99_lab.rst

courses/comprehensive_rust_training/150_borrowing/05_exercise.rst

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=============================
2+
Exercise: Health Statistics
3+
=============================
4+
5+
-----------------------------
6+
Health Statistics Problem
7+
-----------------------------
8+
9+
You're working on implementing a health-monitoring system. As part of that, you
10+
need to keep track of users' health statistics.
11+
12+
You'll start with a stubbed function in an :rust:`impl` block as well as a :rust:`User`
13+
struct definition. Your goal is to implement the stubbed out method on the
14+
:rust:`User` :rust:`struct` defined in the :rust:`impl` block.
15+
16+
Copy the code below to https://play.rust-lang.org/ and fill in the
17+
missing method:
18+
19+
.. container:: source_include 150_borrowing/src/150_borrowing.rs :start-after://ANCHOR-setup :end-before://ANCHOR-solution :code:rust
20+
21+
.. code:: rust
22+
23+
impl User {
24+
pub fn new(name: String, age: u32, height: f32) -> Self {
25+
Self { name, age, height, visit_count: 0, last_blood_pressure: None }
26+
}
27+
28+
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
29+
todo!("Update a user's statistics based on measurements from a visit to the doctor")
30+
}
31+
}
32+
33+
34+
-----------------------------
35+
Health Statistics Main
36+
-----------------------------
37+
38+
.. container:: source_include 150_borrowing/src/150_borrowing.rs :start-after://ANCHOR-main :code:rust
39+
40+
-----------------------------
41+
Health Statistics Solution
42+
-----------------------------
43+
44+
.. container:: source_include 150_borrowing/src/150_borrowing.rs :start-after://ANCHOR-solution :end-before://ANCHOR-main :code:rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//ANCHOR-setup
2+
3+
#![allow(dead_code)]
4+
pub struct User {
5+
name: String,
6+
age: u32,
7+
height: f32,
8+
visit_count: u32,
9+
last_blood_pressure: Option<(u32, u32)>,
10+
}
11+
12+
pub struct Measurements {
13+
height: f32,
14+
blood_pressure: (u32, u32),
15+
}
16+
17+
pub struct HealthReport<'a> {
18+
patient_name: &'a str,
19+
visit_count: u32,
20+
height_change: f32,
21+
blood_pressure_change: Option<(i32, i32)>,
22+
}
23+
24+
//ANCHOR-solution
25+
impl User {
26+
pub fn new(name: String, age: u32, height: f32) -> Self {
27+
Self { name, age, height, visit_count: 0, last_blood_pressure: None }
28+
}
29+
30+
pub fn visit_doctor(&mut self, measurements: Measurements) -> HealthReport {
31+
self.visit_count += 1;
32+
let bp = measurements.blood_pressure;
33+
let report = HealthReport {
34+
patient_name: &self.name,
35+
visit_count: self.visit_count,
36+
height_change: measurements.height - self.height,
37+
blood_pressure_change: match self.last_blood_pressure {
38+
Some(lbp) => {
39+
Some((bp.0 as i32 - lbp.0 as i32, bp.1 as i32 - lbp.1 as i32))
40+
}
41+
None => None,
42+
},
43+
};
44+
self.height = measurements.height;
45+
self.last_blood_pressure = Some(bp);
46+
report
47+
}
48+
}
49+
50+
//ANCHOR-main
51+
fn main() {
52+
let bob = User::new(String::from("Bob"), 32, 155.2);
53+
println!("I'm {} and my age is {}", bob.name, bob.age);
54+
}
55+
56+
#[test]
57+
fn test_visit() {
58+
let mut bob = User::new(String::from("Bob"), 32, 155.2);
59+
assert_eq!(bob.visit_count, 0);
60+
let report =
61+
bob.visit_doctor(Measurements { height: 156.1, blood_pressure: (120, 80) });
62+
assert_eq!(report.patient_name, "Bob");
63+
assert_eq!(report.visit_count, 1);
64+
assert_eq!(report.blood_pressure_change, None);
65+
assert!((report.height_change - 0.9).abs() < 0.00001);
66+
67+
let report =
68+
bob.visit_doctor(Measurements { height: 156.1, blood_pressure: (115, 76) });
69+
70+
assert_eq!(report.visit_count, 2);
71+
assert_eq!(report.blood_pressure_change, Some((-5, -4)));
72+
assert_eq!(report.height_change, 0.0);
73+
}

0 commit comments

Comments
 (0)