mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
Use infallible conversion to teach From trait
This commit is contained in:
parent
9b50da484f
commit
db5ad7f42f
@ -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,49 +2,36 @@
|
|||||||
// 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
|
||||||
|
//
|
||||||
|
// Frank the fairy would like to buy some truffles from Grace the gnome, a
|
||||||
|
// world-renowned chocolatier. The truffles are priced in GnomeCoin though, and
|
||||||
|
// Frank only has FairyCredit. Help Frank by providing a `From` implementation
|
||||||
|
// to convert his FairyCredit to GnomeCoin. At the current exchange rate, one
|
||||||
|
// FairyCredit is valued at 100 GnomeCoin.
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person {
|
struct FairyCredit(u32);
|
||||||
name: String,
|
|
||||||
age: u8,
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct GnomeCoin(u64);
|
||||||
|
|
||||||
|
impl From<FairyCredit> for GnomeCoin {
|
||||||
|
// TODO: implement From<FairyCredit> for GnomeCoin
|
||||||
}
|
}
|
||||||
|
|
||||||
// We implement the Default trait to use it as a fallback when the provided
|
// Note that we shouldn't provide the opposite conversion: from GnomeCoin to
|
||||||
// string is not convertible into a `Person` object.
|
// FairyCredits. That's because less than 100 GnomeCoins cannot be represented
|
||||||
impl Default for Person {
|
// as FairyCredits, which would make the conversion lossy. The `From` trait is
|
||||||
fn default() -> Self {
|
// only appropriate for infallible and lossless conversions.
|
||||||
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:
|
|
||||||
// 1. Split the given string on the commas present in it.
|
|
||||||
// 2. If the split operation returns less or more than 2 elements, return the
|
|
||||||
// default of `Person`.
|
|
||||||
// 3. Use the first element from the split operation as the name.
|
|
||||||
// 4. If the name is empty, return the default of `Person`.
|
|
||||||
// 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<&str> for Person {
|
|
||||||
fn from(s: &str) -> Self {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function.
|
// Use the `from` function.
|
||||||
let p1 = Person::from("Mark,20");
|
let g1 = GnomeCoin::from(FairyCredit(12));
|
||||||
println!("{p1:?}");
|
println!("{g1:?}");
|
||||||
|
|
||||||
// Since `From` is implemented for Person, we are able to use `Into`.
|
// Since `From` is implemented for GnomeCoin, we are able to use `Into`.
|
||||||
let p2: Person = "Gerald,70".into();
|
let g2: GnomeCoin = FairyCredit(9).into();
|
||||||
println!("{p2:?}");
|
println!("{g2:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -52,79 +39,14 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default() {
|
fn test_from() {
|
||||||
let dp = Person::default();
|
let g = GnomeCoin::from(FairyCredit(12));
|
||||||
assert_eq!(dp.name, "John");
|
assert_eq!(g, GnomeCoin(1200));
|
||||||
assert_eq!(dp.age, 30);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_convert() {
|
fn test_into() {
|
||||||
let p = Person::from("");
|
let g: GnomeCoin = FairyCredit(9).into();
|
||||||
assert_eq!(p.name, "John");
|
assert_eq!(g, GnomeCoin(900));
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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,9 @@ 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."""
|
Implement From<FairyCredit> for GnomeCoin. Check the documentation of `From` to
|
||||||
|
learn about its required items:
|
||||||
|
https://doc.rust-lang.org/std/convert/trait.From.html"""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "conversions3"
|
name = "conversions3"
|
||||||
|
|||||||
@ -2,55 +2,38 @@
|
|||||||
// 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
|
||||||
|
//
|
||||||
|
// Frank the fairy would like to buy some truffles from Grace the gnome, a
|
||||||
|
// world-renowned chocolatier. The truffles are priced in GnomeCoin though, and
|
||||||
|
// Frank only has FairyCredit. Help Frank by providing a `From` implementation
|
||||||
|
// to convert his FairyCredit to GnomeCoin. At the current exchange rate, one
|
||||||
|
// FairyCredit is valued at 100 GnomeCoin.
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Person {
|
struct FairyCredit(u32);
|
||||||
name: String,
|
|
||||||
age: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
// We implement the Default trait to use it as a fallback when the provided
|
#[derive(Debug, PartialEq)]
|
||||||
// string is not convertible into a `Person` object.
|
struct GnomeCoin(u64);
|
||||||
impl Default for Person {
|
|
||||||
fn default() -> Self {
|
impl From<FairyCredit> for GnomeCoin {
|
||||||
Self {
|
fn from(value: FairyCredit) -> Self {
|
||||||
name: String::from("John"),
|
Self(value.0 as u64 * 100)
|
||||||
age: 30,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&str> for Person {
|
// Note that we shouldn't provide the opposite conversion: from GnomeCoin to
|
||||||
fn from(s: &str) -> Self {
|
// FairyCredits. That's because less than 100 GnomeCoins cannot be represented
|
||||||
let mut split = s.split(',');
|
// as FairyCredits, which would make the conversion lossy. The `From` trait is
|
||||||
let (Some(name), Some(age), None) = (split.next(), split.next(), split.next()) else {
|
// only appropriate for infallible and lossless conversions.
|
||||||
// ^^^^ 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.
|
// Use the `from` function.
|
||||||
let p1 = Person::from("Mark,20");
|
let g1 = GnomeCoin::from(FairyCredit(12));
|
||||||
println!("{p1:?}");
|
println!("{g1:?}");
|
||||||
|
|
||||||
// Since `From` is implemented for Person, we are able to use `Into`.
|
// Since `From` is implemented for GnomeCoin, we are able to use `Into`.
|
||||||
let p2: Person = "Gerald,70".into();
|
let g2: GnomeCoin = FairyCredit(9).into();
|
||||||
println!("{p2:?}");
|
println!("{g2:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -58,79 +41,14 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default() {
|
fn test_from() {
|
||||||
let dp = Person::default();
|
let g = GnomeCoin::from(FairyCredit(12));
|
||||||
assert_eq!(dp.name, "John");
|
assert_eq!(g, GnomeCoin(1200));
|
||||||
assert_eq!(dp.age, 30);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_convert() {
|
fn test_into() {
|
||||||
let p = Person::from("");
|
let g: GnomeCoin = FairyCredit(9).into();
|
||||||
assert_eq!(p.name, "John");
|
assert_eq!(g, GnomeCoin(900));
|
||||||
assert_eq!(p.age, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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