Skip to content

Commit ef218cd

Browse files
authored
Merge pull request rust-lang#2386 from senekor/senekor/lxuqllrrmroy
Use infallible conversion to teach From trait
2 parents 9b50da4 + 361a7f5 commit ef218cd

4 files changed

Lines changed: 69 additions & 217 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- `structs3`: Rewrote the exercise to make users type method syntax themselves.
2323
- Rename the exercises for smart pointers and conversions so they're sorted alphabetically. [@foxfromworld](https://github.com/foxfromworld)
2424
- `vecs1`: Remove array literal. Some learners assumed their task is to convert it to a vector.
25+
- `conversions2`: Redesign the context such that infallible conversion makes sense.
2526

2627
## 6.5.0 (2025-08-21)
2728

exercises/23_conversions/conversions2.rs

Lines changed: 30 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,53 @@
22
// implemented, an implementation of `Into` is automatically provided.
33
// You can read more about it in the documentation:
44
// https://doc.rust-lang.org/std/convert/trait.From.html
5+
//
6+
// Representing units of measurements with separate types is a common practice.
7+
// It avoids accidentally mixing up values of different units of measurement.
58

6-
#[derive(Debug)]
7-
struct Person {
8-
name: String,
9-
age: u8,
10-
}
9+
struct Celsius(f64);
1110

12-
// We implement the Default trait to use it as a fallback when the provided
13-
// string is not convertible into a `Person` object.
14-
impl Default for Person {
15-
fn default() -> Self {
16-
Self {
17-
name: String::from("John"),
18-
age: 30,
19-
}
20-
}
11+
struct Fahrenheit(f64);
12+
13+
impl From<Celsius> for Fahrenheit {
14+
// TODO: Convert Celsius to Fahrenheit. Don't worry about floating-point
15+
// precision. The formula is: F = C * 1.8 + 32
2116
}
2217

23-
// TODO: Complete this `From` implementation to be able to parse a `Person`
24-
// out of a string in the form of "Mark,20".
25-
// Note that you'll need to parse the age component into a `u8` with something
26-
// like `"4".parse::<u8>()`.
27-
//
28-
// Steps:
29-
// 1. Split the given string on the commas present in it.
30-
// 2. If the split operation returns less or more than 2 elements, return the
31-
// default of `Person`.
32-
// 3. Use the first element from the split operation as the name.
33-
// 4. If the name is empty, return the default of `Person`.
34-
// 5. Parse the second element from the split operation into a `u8` as the age.
35-
// 6. If parsing the age fails, return the default of `Person`.
36-
impl From<&str> for Person {
37-
fn from(s: &str) -> Self {}
18+
impl From<Fahrenheit> for Celsius {
19+
// TODO: Convert Fahrenheit to Celsius.
3820
}
3921

4022
fn main() {
41-
// Use the `from` function.
42-
let p1 = Person::from("Mark,20");
43-
println!("{p1:?}");
44-
45-
// Since `From` is implemented for Person, we are able to use `Into`.
46-
let p2: Person = "Gerald,70".into();
47-
println!("{p2:?}");
23+
// You can optionally experiment here.
4824
}
4925

5026
#[cfg(test)]
5127
mod tests {
5228
use super::*;
5329

54-
#[test]
55-
fn test_default() {
56-
let dp = Person::default();
57-
assert_eq!(dp.name, "John");
58-
assert_eq!(dp.age, 30);
59-
}
60-
61-
#[test]
62-
fn test_bad_convert() {
63-
let p = Person::from("");
64-
assert_eq!(p.name, "John");
65-
assert_eq!(p.age, 30);
66-
}
67-
68-
#[test]
69-
fn test_good_convert() {
70-
let p = Person::from("Mark,20");
71-
assert_eq!(p.name, "Mark");
72-
assert_eq!(p.age, 20);
73-
}
74-
75-
#[test]
76-
fn test_bad_age() {
77-
let p = Person::from("Mark,twenty");
78-
assert_eq!(p.name, "John");
79-
assert_eq!(p.age, 30);
80-
}
30+
const CASES: [(f64, f64); 6] = [
31+
(-50.0, -58.0),
32+
(0.0, 32.0),
33+
(20.0, 68.0),
34+
(100.0, 212.0),
35+
(400.0, 752.0),
36+
(1000.0, 1832.0),
37+
];
8138

8239
#[test]
83-
fn test_missing_comma_and_age() {
84-
let p: Person = Person::from("Mark");
85-
assert_eq!(p.name, "John");
86-
assert_eq!(p.age, 30);
87-
}
88-
89-
#[test]
90-
fn test_missing_age() {
91-
let p: Person = Person::from("Mark,");
92-
assert_eq!(p.name, "John");
93-
assert_eq!(p.age, 30);
94-
}
95-
96-
#[test]
97-
fn test_missing_name() {
98-
let p: Person = Person::from(",1");
99-
assert_eq!(p.name, "John");
100-
assert_eq!(p.age, 30);
101-
}
102-
103-
#[test]
104-
fn test_missing_name_and_age() {
105-
let p: Person = Person::from(",");
106-
assert_eq!(p.name, "John");
107-
assert_eq!(p.age, 30);
108-
}
109-
110-
#[test]
111-
fn test_missing_name_and_invalid_age() {
112-
let p: Person = Person::from(",one");
113-
assert_eq!(p.name, "John");
114-
assert_eq!(p.age, 30);
115-
}
116-
117-
#[test]
118-
fn test_trailing_comma() {
119-
let p: Person = Person::from("Mike,32,");
120-
assert_eq!(p.name, "John");
121-
assert_eq!(p.age, 30);
40+
fn celsius_to_fahrenheit() {
41+
for (celsius, fahrenheit) in CASES {
42+
let Fahrenheit(actual) = Celsius(celsius).into();
43+
assert_eq!(actual.round(), fahrenheit);
44+
}
12245
}
12346

12447
#[test]
125-
fn test_trailing_comma_and_some_string() {
126-
let p: Person = Person::from("Mike,32,dog");
127-
assert_eq!(p.name, "John");
128-
assert_eq!(p.age, 30);
48+
fn fahrenheit_to_celsius() {
49+
for (celsius, fahrenheit) in CASES {
50+
let Celsius(actual) = Fahrenheit(fahrenheit).into();
51+
assert_eq!(actual.round(), celsius);
52+
}
12953
}
13054
}

rustlings-macros/info.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,13 @@ Use the `as` operator to cast one of the operands in the last line of the
11711171
name = "conversions2"
11721172
dir = "23_conversions"
11731173
hint = """
1174-
Follow the steps provided right before the `From` implementation."""
1174+
The formula for converting from Fahrenheit to Celsius is: C = (F - 32) / 1.8
1175+
This can be derived from the first formula:
1176+
1177+
F = C * 1.8 + 32 // now subtract 32 on both sides
1178+
F - 32 = C * 1.8 // then divide by 1.8
1179+
(F - 32) / 1.8 = C
1180+
"""
11751181

11761182
[[exercises]]
11771183
name = "conversions3"

solutions/23_conversions/conversions2.rs

Lines changed: 31 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,135 +2,56 @@
22
// implemented, an implementation of `Into` is automatically provided.
33
// You can read more about it in the documentation:
44
// https://doc.rust-lang.org/std/convert/trait.From.html
5+
//
6+
// Representing units of measurements with separate types is a common practice.
7+
// It avoids accidentally mixing up values of different units of measurement.
58

6-
#[derive(Debug)]
7-
struct Person {
8-
name: String,
9-
age: u8,
10-
}
9+
struct Celsius(f64);
1110

12-
// We implement the Default trait to use it as a fallback when the provided
13-
// string is not convertible into a `Person` object.
14-
impl Default for Person {
15-
fn default() -> Self {
16-
Self {
17-
name: String::from("John"),
18-
age: 30,
19-
}
11+
struct Fahrenheit(f64);
12+
13+
impl From<Celsius> for Fahrenheit {
14+
fn from(Celsius(celsius): Celsius) -> Self {
15+
Fahrenheit(celsius * 1.8 + 32.0)
2016
}
2117
}
2218

23-
impl From<&str> for Person {
24-
fn from(s: &str) -> Self {
25-
let mut split = s.split(',');
26-
let (Some(name), Some(age), None) = (split.next(), split.next(), split.next()) else {
27-
// ^^^^ there should be no third element
28-
return Self::default();
29-
};
30-
31-
if name.is_empty() {
32-
return Self::default();
33-
}
34-
35-
let Ok(age) = age.parse() else {
36-
return Self::default();
37-
};
38-
39-
Self {
40-
name: name.into(),
41-
age,
42-
}
19+
impl From<Fahrenheit> for Celsius {
20+
fn from(Fahrenheit(fahrenheit): Fahrenheit) -> Self {
21+
Celsius((fahrenheit - 32.0) / 1.8)
4322
}
4423
}
4524

4625
fn main() {
47-
// Use the `from` function.
48-
let p1 = Person::from("Mark,20");
49-
println!("{p1:?}");
50-
51-
// Since `From` is implemented for Person, we are able to use `Into`.
52-
let p2: Person = "Gerald,70".into();
53-
println!("{p2:?}");
26+
// You can optionally experiment here.
5427
}
5528

5629
#[cfg(test)]
5730
mod tests {
5831
use super::*;
5932

60-
#[test]
61-
fn test_default() {
62-
let dp = Person::default();
63-
assert_eq!(dp.name, "John");
64-
assert_eq!(dp.age, 30);
65-
}
33+
const CASES: [(f64, f64); 6] = [
34+
(-50.0, -58.0),
35+
(0.0, 32.0),
36+
(20.0, 68.0),
37+
(100.0, 212.0),
38+
(400.0, 752.0),
39+
(1000.0, 1832.0),
40+
];
6641

6742
#[test]
68-
fn test_bad_convert() {
69-
let p = Person::from("");
70-
assert_eq!(p.name, "John");
71-
assert_eq!(p.age, 30);
72-
}
73-
74-
#[test]
75-
fn test_good_convert() {
76-
let p = Person::from("Mark,20");
77-
assert_eq!(p.name, "Mark");
78-
assert_eq!(p.age, 20);
79-
}
80-
81-
#[test]
82-
fn test_bad_age() {
83-
let p = Person::from("Mark,twenty");
84-
assert_eq!(p.name, "John");
85-
assert_eq!(p.age, 30);
86-
}
87-
88-
#[test]
89-
fn test_missing_comma_and_age() {
90-
let p: Person = Person::from("Mark");
91-
assert_eq!(p.name, "John");
92-
assert_eq!(p.age, 30);
93-
}
94-
95-
#[test]
96-
fn test_missing_age() {
97-
let p: Person = Person::from("Mark,");
98-
assert_eq!(p.name, "John");
99-
assert_eq!(p.age, 30);
100-
}
101-
102-
#[test]
103-
fn test_missing_name() {
104-
let p: Person = Person::from(",1");
105-
assert_eq!(p.name, "John");
106-
assert_eq!(p.age, 30);
107-
}
108-
109-
#[test]
110-
fn test_missing_name_and_age() {
111-
let p: Person = Person::from(",");
112-
assert_eq!(p.name, "John");
113-
assert_eq!(p.age, 30);
114-
}
115-
116-
#[test]
117-
fn test_missing_name_and_invalid_age() {
118-
let p: Person = Person::from(",one");
119-
assert_eq!(p.name, "John");
120-
assert_eq!(p.age, 30);
121-
}
122-
123-
#[test]
124-
fn test_trailing_comma() {
125-
let p: Person = Person::from("Mike,32,");
126-
assert_eq!(p.name, "John");
127-
assert_eq!(p.age, 30);
43+
fn celsius_to_fahrenheit() {
44+
for (celsius, fahrenheit) in CASES {
45+
let Fahrenheit(actual) = Celsius(celsius).into();
46+
assert_eq!(actual.round(), fahrenheit);
47+
}
12848
}
12949

13050
#[test]
131-
fn test_trailing_comma_and_some_string() {
132-
let p: Person = Person::from("Mike,32,dog");
133-
assert_eq!(p.name, "John");
134-
assert_eq!(p.age, 30);
51+
fn fahrenheit_to_celsius() {
52+
for (celsius, fahrenheit) in CASES {
53+
let Celsius(actual) = Fahrenheit(fahrenheit).into();
54+
assert_eq!(actual.round(), celsius);
55+
}
13556
}
13657
}

0 commit comments

Comments
 (0)