mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
Merge pull request #2386 from senekor/senekor/lxuqllrrmroy
Use infallible conversion to teach From trait
This commit is contained in:
commit
ef218cd5d0
@ -22,6 +22,7 @@
|
|||||||
- `structs3`: Rewrote the exercise to make users type method syntax themselves.
|
- `structs3`: Rewrote the exercise to make users type method syntax themselves.
|
||||||
- Rename the exercises for smart pointers and conversions so they're sorted alphabetically. [@foxfromworld](https://github.com/foxfromworld)
|
- Rename the exercises for smart pointers and conversions so they're sorted alphabetically. [@foxfromworld](https://github.com/foxfromworld)
|
||||||
- `vecs1`: Remove array literal. Some learners assumed their task is to convert it to a vector.
|
- `vecs1`: Remove array literal. Some learners assumed their task is to convert it to a vector.
|
||||||
|
- `conversions2`: Redesign the context such that infallible conversion makes sense.
|
||||||
|
|
||||||
## 6.5.0 (2025-08-21)
|
## 6.5.0 (2025-08-21)
|
||||||
|
|
||||||
|
|||||||
@ -2,129 +2,53 @@
|
|||||||
// implemented, an implementation of `Into` is automatically provided.
|
// implemented, an implementation of `Into` is automatically provided.
|
||||||
// You can read more about it in the documentation:
|
// You can read more about it in the documentation:
|
||||||
// https://doc.rust-lang.org/std/convert/trait.From.html
|
// https://doc.rust-lang.org/std/convert/trait.From.html
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
// We implement the Default trait to use it as a fallback when the provided
|
|
||||||
// string is not convertible into a `Person` object.
|
|
||||||
impl Default for Person {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
name: String::from("John"),
|
|
||||||
age: 30,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Complete this `From` implementation to be able to parse a `Person`
|
|
||||||
// out of a string in the form of "Mark,20".
|
|
||||||
// Note that you'll need to parse the age component into a `u8` with something
|
|
||||||
// like `"4".parse::<u8>()`.
|
|
||||||
//
|
//
|
||||||
// Steps:
|
// Representing units of measurements with separate types is a common practice.
|
||||||
// 1. Split the given string on the commas present in it.
|
// It avoids accidentally mixing up values of different units of measurement.
|
||||||
// 2. If the split operation returns less or more than 2 elements, return the
|
|
||||||
// default of `Person`.
|
struct Celsius(f64);
|
||||||
// 3. Use the first element from the split operation as the name.
|
|
||||||
// 4. If the name is empty, return the default of `Person`.
|
struct Fahrenheit(f64);
|
||||||
// 5. Parse the second element from the split operation into a `u8` as the age.
|
|
||||||
// 6. If parsing the age fails, return the default of `Person`.
|
impl From<Celsius> for Fahrenheit {
|
||||||
impl From<&str> for Person {
|
// TODO: Convert Celsius to Fahrenheit. Don't worry about floating-point
|
||||||
fn from(s: &str) -> Self {}
|
// precision. The formula is: F = C * 1.8 + 32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Fahrenheit> for Celsius {
|
||||||
|
// TODO: Convert Fahrenheit to Celsius.
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function.
|
// You can optionally experiment here.
|
||||||
let p1 = Person::from("Mark,20");
|
|
||||||
println!("{p1:?}");
|
|
||||||
|
|
||||||
// Since `From` is implemented for Person, we are able to use `Into`.
|
|
||||||
let p2: Person = "Gerald,70".into();
|
|
||||||
println!("{p2:?}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
const CASES: [(f64, f64); 6] = [
|
||||||
|
(-50.0, -58.0),
|
||||||
|
(0.0, 32.0),
|
||||||
|
(20.0, 68.0),
|
||||||
|
(100.0, 212.0),
|
||||||
|
(400.0, 752.0),
|
||||||
|
(1000.0, 1832.0),
|
||||||
|
];
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default() {
|
fn celsius_to_fahrenheit() {
|
||||||
let dp = Person::default();
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(dp.name, "John");
|
let Fahrenheit(actual) = Celsius(celsius).into();
|
||||||
assert_eq!(dp.age, 30);
|
assert_eq!(actual.round(), fahrenheit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_convert() {
|
fn fahrenheit_to_celsius() {
|
||||||
let p = Person::from("");
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(p.name, "John");
|
let Celsius(actual) = Fahrenheit(fahrenheit).into();
|
||||||
assert_eq!(p.age, 30);
|
assert_eq!(actual.round(), celsius);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_good_convert() {
|
|
||||||
let p = Person::from("Mark,20");
|
|
||||||
assert_eq!(p.name, "Mark");
|
|
||||||
assert_eq!(p.age, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_bad_age() {
|
|
||||||
let p = Person::from("Mark,twenty");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_comma_and_age() {
|
|
||||||
let p: Person = Person::from("Mark");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_age() {
|
|
||||||
let p: Person = Person::from("Mark,");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name() {
|
|
||||||
let p: Person = Person::from(",1");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name_and_age() {
|
|
||||||
let p: Person = Person::from(",");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name_and_invalid_age() {
|
|
||||||
let p: Person = Person::from(",one");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_trailing_comma() {
|
|
||||||
let p: Person = Person::from("Mike,32,");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_trailing_comma_and_some_string() {
|
|
||||||
let p: Person = Person::from("Mike,32,dog");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1171,7 +1171,13 @@ Use the `as` operator to cast one of the operands in the last line of the
|
|||||||
name = "conversions2"
|
name = "conversions2"
|
||||||
dir = "23_conversions"
|
dir = "23_conversions"
|
||||||
hint = """
|
hint = """
|
||||||
Follow the steps provided right before the `From` implementation."""
|
The formula for converting from Fahrenheit to Celsius is: C = (F - 32) / 1.8
|
||||||
|
This can be derived from the first formula:
|
||||||
|
|
||||||
|
F = C * 1.8 + 32 // now subtract 32 on both sides
|
||||||
|
F - 32 = C * 1.8 // then divide by 1.8
|
||||||
|
(F - 32) / 1.8 = C
|
||||||
|
"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "conversions3"
|
name = "conversions3"
|
||||||
|
|||||||
@ -2,135 +2,56 @@
|
|||||||
// implemented, an implementation of `Into` is automatically provided.
|
// implemented, an implementation of `Into` is automatically provided.
|
||||||
// You can read more about it in the documentation:
|
// You can read more about it in the documentation:
|
||||||
// https://doc.rust-lang.org/std/convert/trait.From.html
|
// https://doc.rust-lang.org/std/convert/trait.From.html
|
||||||
|
//
|
||||||
|
// Representing units of measurements with separate types is a common practice.
|
||||||
|
// It avoids accidentally mixing up values of different units of measurement.
|
||||||
|
|
||||||
#[derive(Debug)]
|
struct Celsius(f64);
|
||||||
struct Person {
|
|
||||||
name: String,
|
|
||||||
age: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
// We implement the Default trait to use it as a fallback when the provided
|
struct Fahrenheit(f64);
|
||||||
// string is not convertible into a `Person` object.
|
|
||||||
impl Default for Person {
|
impl From<Celsius> for Fahrenheit {
|
||||||
fn default() -> Self {
|
fn from(Celsius(celsius): Celsius) -> Self {
|
||||||
Self {
|
Fahrenheit(celsius * 1.8 + 32.0)
|
||||||
name: String::from("John"),
|
|
||||||
age: 30,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Person {
|
impl From<Fahrenheit> for Celsius {
|
||||||
fn from(s: &str) -> Self {
|
fn from(Fahrenheit(fahrenheit): Fahrenheit) -> Self {
|
||||||
let mut split = s.split(',');
|
Celsius((fahrenheit - 32.0) / 1.8)
|
||||||
let (Some(name), Some(age), None) = (split.next(), split.next(), split.next()) else {
|
|
||||||
// ^^^^ there should be no third element
|
|
||||||
return Self::default();
|
|
||||||
};
|
|
||||||
|
|
||||||
if name.is_empty() {
|
|
||||||
return Self::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
let Ok(age) = age.parse() else {
|
|
||||||
return Self::default();
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
|
||||||
name: name.into(),
|
|
||||||
age,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function.
|
// You can optionally experiment here.
|
||||||
let p1 = Person::from("Mark,20");
|
|
||||||
println!("{p1:?}");
|
|
||||||
|
|
||||||
// Since `From` is implemented for Person, we are able to use `Into`.
|
|
||||||
let p2: Person = "Gerald,70".into();
|
|
||||||
println!("{p2:?}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
const CASES: [(f64, f64); 6] = [
|
||||||
|
(-50.0, -58.0),
|
||||||
|
(0.0, 32.0),
|
||||||
|
(20.0, 68.0),
|
||||||
|
(100.0, 212.0),
|
||||||
|
(400.0, 752.0),
|
||||||
|
(1000.0, 1832.0),
|
||||||
|
];
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default() {
|
fn celsius_to_fahrenheit() {
|
||||||
let dp = Person::default();
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(dp.name, "John");
|
let Fahrenheit(actual) = Celsius(celsius).into();
|
||||||
assert_eq!(dp.age, 30);
|
assert_eq!(actual.round(), fahrenheit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_convert() {
|
fn fahrenheit_to_celsius() {
|
||||||
let p = Person::from("");
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(p.name, "John");
|
let Celsius(actual) = Fahrenheit(fahrenheit).into();
|
||||||
assert_eq!(p.age, 30);
|
assert_eq!(actual.round(), celsius);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_good_convert() {
|
|
||||||
let p = Person::from("Mark,20");
|
|
||||||
assert_eq!(p.name, "Mark");
|
|
||||||
assert_eq!(p.age, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_bad_age() {
|
|
||||||
let p = Person::from("Mark,twenty");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_comma_and_age() {
|
|
||||||
let p: Person = Person::from("Mark");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_age() {
|
|
||||||
let p: Person = Person::from("Mark,");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name() {
|
|
||||||
let p: Person = Person::from(",1");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name_and_age() {
|
|
||||||
let p: Person = Person::from(",");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_missing_name_and_invalid_age() {
|
|
||||||
let p: Person = Person::from(",one");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_trailing_comma() {
|
|
||||||
let p: Person = Person::from("Mike,32,");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_trailing_comma_and_some_string() {
|
|
||||||
let p: Person = Person::from("Mike,32,dog");
|
|
||||||
assert_eq!(p.name, "John");
|
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user