mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
Simplify story and increase difficulty
Conversion between Celsius and Fahrenheit should be understandable to most. Inverting the formula is still not very hard, but a little harder than only multiplying by 100.
This commit is contained in:
parent
db5ad7f42f
commit
360344ab6c
@ -3,50 +3,52 @@
|
|||||||
// 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
|
// Representing units of measurements with separate types is a common practice.
|
||||||
// world-renowned chocolatier. The truffles are priced in GnomeCoin though, and
|
// It avoids accidentally mixing up values of different units of measurement.
|
||||||
// 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)]
|
struct Celsius(f64);
|
||||||
struct FairyCredit(u32);
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
struct Fahrenheit(f64);
|
||||||
struct GnomeCoin(u64);
|
|
||||||
|
|
||||||
impl From<FairyCredit> for GnomeCoin {
|
impl From<Celsius> for Fahrenheit {
|
||||||
// TODO: implement From<FairyCredit> for GnomeCoin
|
// TODO: Convert Celsius to Fahrenheit. Don't worry about floating-point
|
||||||
|
// precision. The formula is: F = C * 1.8 + 32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that we shouldn't provide the opposite conversion: from GnomeCoin to
|
impl From<Fahrenheit> for Celsius {
|
||||||
// FairyCredits. That's because less than 100 GnomeCoins cannot be represented
|
// TODO: Convert Fahrenheit to Celsius.
|
||||||
// as FairyCredits, which would make the conversion lossy. The `From` trait is
|
}
|
||||||
// only appropriate for infallible and lossless conversions.
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function.
|
// You can optionally experiment here.
|
||||||
let g1 = GnomeCoin::from(FairyCredit(12));
|
|
||||||
println!("{g1:?}");
|
|
||||||
|
|
||||||
// Since `From` is implemented for GnomeCoin, we are able to use `Into`.
|
|
||||||
let g2: GnomeCoin = FairyCredit(9).into();
|
|
||||||
println!("{g2:?}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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_from() {
|
fn celsius_to_fahrenheit() {
|
||||||
let g = GnomeCoin::from(FairyCredit(12));
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(g, GnomeCoin(1200));
|
let Fahrenheit(actual) = Celsius(celsius).into();
|
||||||
|
assert_eq!(actual.round(), fahrenheit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into() {
|
fn fahrenheit_to_celsius() {
|
||||||
let g: GnomeCoin = FairyCredit(9).into();
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(g, GnomeCoin(900));
|
let Celsius(actual) = Fahrenheit(fahrenheit).into();
|
||||||
|
assert_eq!(actual.round(), celsius);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1171,9 +1171,8 @@ 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 = """
|
||||||
Implement From<FairyCredit> for GnomeCoin. Check the documentation of `From` to
|
For the conversion from Fahrenheit to Celsius, you have to determine the formula
|
||||||
learn about its required items:
|
yourself. Don't forget the order of operations!"""
|
||||||
https://doc.rust-lang.org/std/convert/trait.From.html"""
|
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "conversions3"
|
name = "conversions3"
|
||||||
|
|||||||
@ -3,52 +3,55 @@
|
|||||||
// 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
|
// Representing units of measurements with separate types is a common practice.
|
||||||
// world-renowned chocolatier. The truffles are priced in GnomeCoin though, and
|
// It avoids accidentally mixing up values of different units of measurement.
|
||||||
// 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)]
|
struct Celsius(f64);
|
||||||
struct FairyCredit(u32);
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
struct Fahrenheit(f64);
|
||||||
struct GnomeCoin(u64);
|
|
||||||
|
|
||||||
impl From<FairyCredit> for GnomeCoin {
|
impl From<Celsius> for Fahrenheit {
|
||||||
fn from(value: FairyCredit) -> Self {
|
fn from(Celsius(celsius): Celsius) -> Self {
|
||||||
Self(value.0 as u64 * 100)
|
Fahrenheit(celsius * 1.8 + 32.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note that we shouldn't provide the opposite conversion: from GnomeCoin to
|
impl From<Fahrenheit> for Celsius {
|
||||||
// FairyCredits. That's because less than 100 GnomeCoins cannot be represented
|
fn from(Fahrenheit(fahrenheit): Fahrenheit) -> Self {
|
||||||
// as FairyCredits, which would make the conversion lossy. The `From` trait is
|
Celsius((fahrenheit - 32.0) / 1.8)
|
||||||
// only appropriate for infallible and lossless conversions.
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Use the `from` function.
|
// You can optionally experiment here.
|
||||||
let g1 = GnomeCoin::from(FairyCredit(12));
|
|
||||||
println!("{g1:?}");
|
|
||||||
|
|
||||||
// Since `From` is implemented for GnomeCoin, we are able to use `Into`.
|
|
||||||
let g2: GnomeCoin = FairyCredit(9).into();
|
|
||||||
println!("{g2:?}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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_from() {
|
fn celsius_to_fahrenheit() {
|
||||||
let g = GnomeCoin::from(FairyCredit(12));
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(g, GnomeCoin(1200));
|
let Fahrenheit(actual) = Celsius(celsius).into();
|
||||||
|
assert_eq!(actual.round(), fahrenheit);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_into() {
|
fn fahrenheit_to_celsius() {
|
||||||
let g: GnomeCoin = FairyCredit(9).into();
|
for (celsius, fahrenheit) in CASES {
|
||||||
assert_eq!(g, GnomeCoin(900));
|
let Celsius(actual) = Fahrenheit(fahrenheit).into();
|
||||||
|
assert_eq!(actual.round(), celsius);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user