Skip to content

Commit 3a8b3c8

Browse files
Merge branch 'mr/481-create-lab-for-module-references' into 'master'
Resolve "Create Lab for Module: References" Closes #481 See merge request feng/training/material!567
2 parents c800d57 + 3a451c8 commit 3a8b3c8

5 files changed

Lines changed: 239 additions & 8 deletions

File tree

courses/rust_essentials/060_references/88-references.lab.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
Lab
33
=====
44

5-
-----
6-
Lab
7-
-----
5+
------------------
6+
Lab Instructions
7+
------------------
8+
9+
- Solve for compilation errors
10+
11+
- Follow the hints!
12+
13+
- Success is
14+
15+
- Code that compiles
816

9-
TBD
17+
- ...and that follows any behavior indicated within the hints!

courses/rust_essentials/060_references/lab/answer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lab"
2+
name = "answer"
33
version = "1.0.0"
44
edition = "2024"
55

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,114 @@
1+
//! Lab (answer)
2+
//! References
3+
//!
4+
15
fn main() {
2-
println!("TBD");
6+
struct ReferencesStruct {
7+
count: i8,
8+
value: f64,
9+
}
10+
11+
// Task 1 Goal
12+
// Use a reference to show changes in the pointed-to object
13+
// Hint
14+
// Cannot modify a shared reference directly
15+
println!("Task 1 - Observer");
16+
let mut angle = ReferencesStruct {
17+
count: 12,
18+
value: 34.5,
19+
};
20+
let ref_angle = ∠
21+
println!("Before: {} {}", ref_angle.count, ref_angle.value);
22+
angle.count *= 2;
23+
angle.value *= 2.0;
24+
let ref_angle = ∠
25+
println!("After: {} {}", ref_angle.count, ref_angle.value);
26+
27+
// Task 2 Goal
28+
// Use a single reference to print the values of multiple pointed-to objects
29+
// Hint
30+
// A shared reference can be mutable!
31+
println!("Task 2 - Rebinder");
32+
let bricks = ReferencesStruct {
33+
count: 6,
34+
value: 78.9,
35+
};
36+
let buildings = ReferencesStruct {
37+
count: 1,
38+
value: 2.3,
39+
};
40+
let mut ref_both = &bricks;
41+
println!("Bricks: {} {}", ref_both.count, ref_both.value);
42+
ref_both = &buildings;
43+
println!("Buildings: {} {}", ref_both.count, ref_both.value);
44+
45+
// Task 3 Goal
46+
// Use a reference to change the value of a pointed-to object
47+
// Hints
48+
// The original object must be modifiable
49+
// The reference object must allow for modification of the contents
50+
println!("Task 3 - Modifier");
51+
let mut cars = ReferencesStruct {
52+
count: 67,
53+
value: 8.9,
54+
};
55+
println!("Before: {} {}", cars.count, cars.value);
56+
let ref_cars = &mut cars;
57+
ref_cars.count = ref_cars.count / 2;
58+
ref_cars.value = ref_cars.value / 2.0;
59+
println!("After: {} {}", cars.count, cars.value);
60+
61+
// Task 4 Goal
62+
// Create a single reference object to modify multiple pointed-to objects
63+
// Hint
64+
// Both the variable holding the reference AND the reference itself must be mutable
65+
println!("Task 4 - Free Agent");
66+
let mut dog = ReferencesStruct {
67+
count: 1,
68+
value: 2.3,
69+
};
70+
let mut duck = ReferencesStruct {
71+
count: 4,
72+
value: 5.6,
73+
};
74+
let mut ref_both: &mut ReferencesStruct = &mut dog;
75+
println!("Before dog: {} {}", ref_both.count, ref_both.value);
76+
*ref_both = ReferencesStruct {
77+
count: 0,
78+
value: 0.0,
79+
};
80+
println!("After dog: {} {}", ref_both.count, ref_both.value);
81+
82+
ref_both = &mut duck;
83+
println!("Before duck: {} {}", ref_both.count, ref_both.value);
84+
*ref_both = ReferencesStruct {
85+
count: 0,
86+
value: 0.0,
87+
};
88+
println!("After duck: {} {}", ref_both.count, ref_both.value);
89+
90+
// Task 5 Goal
91+
// Modify the existing code to print correctly
92+
// Hint
93+
// Need to make the referenced object last longer
94+
println!("Task 5 - Dangling Reference");
95+
let jack = String::from("Jack");
96+
let rose = {
97+
&jack // 'rose' is a reference to 'jack'
98+
};
99+
println!("Jack screams '{rose}'");
100+
101+
// Task 6 Goal
102+
// Use a single reference object to print all different length slices of 'floats'
103+
// Hint
104+
// Reference object refers to a slice of the object, not length
105+
// "{:?}" is used to print the contents of the array (Debug)
106+
println!("Task 6 - Slices");
107+
let floats: [f64; 5] = [1.1, 22.22, 333.333, 4444.4444, 55555.555555];
108+
let mut ref_floats: &[f64] = &floats[0..1];
109+
println!("{:?}", ref_floats);
110+
ref_floats = &floats[1..3];
111+
println!("{:?}", ref_floats);
112+
ref_floats = &floats[2..];
113+
println!("{:?}", ref_floats);
3114
}

courses/rust_essentials/060_references/lab/prompt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "lab"
2+
name = "prompt"
33
version = "1.0.0"
44
edition = "2024"
55

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,115 @@
1+
//! Lab (prompt)
2+
//! References
3+
//!
4+
//! Fix all the compile errors below by following the hints provided
5+
//!
6+
17
fn main() {
2-
println!("TBD");
8+
struct ReferencesStruct {
9+
count: i8,
10+
value: f64,
11+
}
12+
13+
// Task 1 Goal
14+
// Use a reference to show changes in the pointed-to value
15+
// Hint
16+
// Cannot modify a shared reference directly
17+
println!("Task 1 - Observer");
18+
let mut angle = ReferencesStruct {
19+
count: 12,
20+
value: 34.5,
21+
};
22+
let ref_angle = ∠
23+
println!("Before: {} {}", ref_angle.count, ref_angle.value);
24+
ref_angle.count *= 2;
25+
ref_angle.value *= 2.0;
26+
println!("After: {} {}", ref_angle.count, ref_angle.value);
27+
28+
// Task 2 Goal
29+
// Use a single reference to print the values of multiple pointed-to objects
30+
// Hint
31+
// A shared reference can be mutable!
32+
println!("Task 2 - Rebinder");
33+
let bricks = ReferencesStruct {
34+
count: 6,
35+
value: 78.9,
36+
};
37+
let buildings = ReferencesStruct {
38+
count: 1,
39+
value: 2.3,
40+
};
41+
let ref_both = &bricks;
42+
println!("Bricks: {} {}", ref_both.count, ref_both.value);
43+
ref_both = &buildings;
44+
println!("Buildings: {} {}", ref_both.count, ref_both.value);
45+
46+
// Task 3 Goal
47+
// Use a reference to change the value of a pointed-to object
48+
// Hint
49+
// The original object must be modifiable
50+
// The reference object must allow for modification of the contents
51+
println!("Task 3 - Modifier");
52+
let cars = ReferencesStruct {
53+
count: 67,
54+
value: 8.9,
55+
};
56+
println!("Before: {} {}", cars.count, cars.value);
57+
let ref_cars = &cars;
58+
ref_cars.count = ref_cars.count / 2;
59+
ref_cars.value = ref_cars.value / 2.0;
60+
println!("After: {} {}", cars.count, cars.value);
61+
62+
// Task 4 Goal
63+
// Create a single reference object to modify multiple pointed-to objects
64+
// Hint
65+
// Both the variable holding the reference AND the reference itself must be mutable
66+
println!("Task 4 - Free Agent");
67+
let dog = ReferencesStruct {
68+
count: 1,
69+
value: 2.3,
70+
};
71+
let duck = ReferencesStruct {
72+
count: 4,
73+
value: 5.6,
74+
};
75+
let ref_both: &ReferencesStruct = &dog;
76+
println!("Before dog: {} {}", ref_both.count, ref_both.value);
77+
*ref_both = ReferencesStruct {
78+
count: 0,
79+
value: 0.0,
80+
};
81+
println!("After dog: {} {}", ref_both.count, ref_both.value);
82+
83+
ref_both = &duck;
84+
println!("Before duck: {} {}", ref_both.count, ref_both.value);
85+
*ref_both = ReferencesStruct {
86+
count: 0,
87+
value: 0.0,
88+
};
89+
println!("After duck: {} {}", ref_both.count, ref_both.value);
90+
91+
// Task 5 Goal
92+
// Modify the existing code to print correctly
93+
// Hint
94+
// Need to make the referenced object last longer
95+
println!("Task 5 - Dangling Reference");
96+
let rose = {
97+
let jack = String::from("Jack");
98+
&jack // 'rose' is a reference to 'jack'
99+
};
100+
println!("Jack screams '{rose}'");
101+
102+
// Task 6 Goal
103+
// Use a single reference object to print all different length slices of 'floats'
104+
// Hint
105+
// Reference object refers to a slice of the object, not length
106+
// "{:?}" is used to print the contents of the array (Debug)
107+
println!("Task 6 - Slices");
108+
let floats: [f64; 5] = [1.1, 22.22, 333.333, 4444.4444, 55555.555555];
109+
let mut ref_floats: &[f64] = &floats;
110+
println!("{:?}", ref_floats);
111+
ref_floats = &floats;
112+
println!("{:?}", ref_floats);
113+
ref_floats = &floats;
114+
println!("{:?}", ref_floats);
3115
}

0 commit comments

Comments
 (0)