Skip to content

Commit 00ad395

Browse files
committed
replace partialeq float temp example to use ints & distances
1 parent a3a22c3 commit 00ad395

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

posts/tour-of-rusts-standard-library-traits.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,58 +2071,58 @@ fn main() {
20712071
}
20722072
```
20732073

2074-
A good example of impling `PartialEq` to check equality between different types would be a program that works with temperature data and uses different types to represent different units of temperature measurement.
2074+
A good example of impling `PartialEq` to check equality between different types would be a program that works with distances and uses different types to represent different units of measurement.
20752075

20762076
```rust
20772077
#[derive(PartialEq)]
2078-
struct Celsius(f64);
2078+
struct Foot(u32);
20792079

20802080
#[derive(PartialEq)]
2081-
struct Kelvin(f64);
2081+
struct Yard(u32);
20822082

20832083
#[derive(PartialEq)]
2084-
struct Fahrenheit(f64);
2084+
struct Mile(u32);
20852085

2086-
impl PartialEq<Fahrenheit> for Celsius {
2087-
fn eq(&self, other: &Fahrenheit) -> bool {
2088-
self.0 == (other.0 - 32.0) * 5.0 / 9.0
2086+
impl PartialEq<Mile> for Foot {
2087+
fn eq(&self, other: &Mile) -> bool {
2088+
self.0 == other.0 * 5280
20892089
}
20902090
}
20912091

2092-
impl PartialEq<Celsius> for Fahrenheit {
2093-
fn eq(&self, other: &Celsius) -> bool {
2094-
self.0 == (other.0 * 9.0 / 5.0) + 32.0
2092+
impl PartialEq<Foot> for Mile {
2093+
fn eq(&self, other: &Foot) -> bool {
2094+
self.0 * 5280 == other.0
20952095
}
20962096
}
20972097

2098-
impl PartialEq<Fahrenheit> for Kelvin {
2099-
fn eq(&self, other: &Fahrenheit) -> bool {
2100-
self.0 == ((other.0 - 32.0) * 5.0 / 9.0) + 273.15
2098+
impl PartialEq<Mile> for Yard {
2099+
fn eq(&self, other: &Mile) -> bool {
2100+
self.0 == other.0 * 1760
21012101
}
21022102
}
21032103

2104-
impl PartialEq<Kelvin> for Fahrenheit {
2105-
fn eq(&self, other: &Kelvin) -> bool {
2106-
self.0 == ((other.0 - 273.15) * 9.0 / 5.0) + 32.0
2104+
impl PartialEq<Yard> for Mile {
2105+
fn eq(&self, other: &Yard) -> bool {
2106+
self.0 * 1760 == other.0
21072107
}
21082108
}
21092109

2110-
impl PartialEq<Celsius> for Kelvin {
2111-
fn eq(&self, other: &Celsius) -> bool {
2112-
self.0 == (other.0 + 273.15)
2110+
impl PartialEq<Foot> for Yard {
2111+
fn eq(&self, other: &Foot) -> bool {
2112+
self.0 * 3 == other.0
21132113
}
21142114
}
21152115

2116-
impl PartialEq<Kelvin> for Celsius {
2117-
fn eq(&self, other: &Kelvin) -> bool {
2118-
self.0 == (other.0 - 273.15)
2116+
impl PartialEq<Yard> for Foot {
2117+
fn eq(&self, other: &Yard) -> bool {
2118+
self.0 == other.0 * 3
21192119
}
21202120
}
21212121

21222122
fn main() {
2123-
let a = Celsius(0.0);
2124-
let b = Kelvin(273.15);
2125-
let c = Fahrenheit(32.0);
2123+
let a = Foot(5280);
2124+
let b = Yard(1760);
2125+
let c = Mile(1);
21262126

21272127
// symmetry
21282128
assert!(a == b && b == a); //

0 commit comments

Comments
 (0)