Comments (2)
I think this is better, I'll be honest, I was just curious if it did work, seeing floating points and == always sets of my "smell test", so the important part is seeing what it looks like generally.
Technically, this can overflow, but I'm not sure it worth fixing that, for a simple example -- 2^32 inches will cover any distance on Earth, and people shouldn't be measuring inter-stellar distances in inches ;)
from rust-blog.
Oof, yeah, I was worried about this, I should have stress-tested that example more. I switched it to distances, this is the example in the blog post now:
#[derive(PartialEq)]
struct Foot(u32);
#[derive(PartialEq)]
struct Yard(u32);
#[derive(PartialEq)]
struct Mile(u32);
impl PartialEq<Mile> for Foot {
fn eq(&self, other: &Mile) -> bool {
self.0 == other.0 * 5280
}
}
impl PartialEq<Foot> for Mile {
fn eq(&self, other: &Foot) -> bool {
self.0 * 5280 == other.0
}
}
impl PartialEq<Mile> for Yard {
fn eq(&self, other: &Mile) -> bool {
self.0 == other.0 * 1760
}
}
impl PartialEq<Yard> for Mile {
fn eq(&self, other: &Yard) -> bool {
self.0 * 1760 == other.0
}
}
impl PartialEq<Foot> for Yard {
fn eq(&self, other: &Foot) -> bool {
self.0 * 3 == other.0
}
}
impl PartialEq<Yard> for Foot {
fn eq(&self, other: &Yard) -> bool {
self.0 == other.0 * 3
}
}
fn main() {
let a = Foot(5280);
let b = Yard(1760);
let c = Mile(1);
// symmetry
assert!(a == b && b == a); // ✅
assert!(b == c && c == b); // ✅
assert!(a == c && c == a); // ✅
// transitivity
assert!(a == b && b == c && a == c); // ✅
assert!(c == b && b == a && c == a); // ✅
}
This is probably better than the temperature example, but it's still unideal because if someone was realistically writing this program they would use f64
s and not u32
s. I'm open to suggestions on how maybe this example can be improved further, or replaced for something better, so I'll leave this issue open for now.
from rust-blog.
Related Issues (20)
- Is it worth to mention about "Polonious"? HOT 1
- Remove static mut example HOT 8
- subjective issue, idk what title to give HOT 3
- Confuse: lifetime with struct and self. HOT 2
- A potential tweak to common-rust-lifetime-misconceptions HOT 1
- Sized is not an auto trait HOT 1
- Paragraph about `T: 'static` is not right HOT 8
- Mention that Read & Write aren't in the prelude when talking about Generic blanket impls HOT 4
- My benchmark results HOT 5
- (nit) Update description of example provided to explain the Clone trait. HOT 1
- Not PostgreSQL but PostgresQL on purpose? HOT 1
- Request for additional example in closure/function lifetime elision distinction HOT 2
- `asymmetry` requirements in `PartialOrd`
- Customer review for Rust on the eve of 2022
- Misconception discussion: T is a superset of both &T and &mut T
- Struct methods can also NOT have elided lifetimes right?
- Request to add extra info: cannot implement both From and TryFrom
- More iteration trait wisdom
- tnanks the series high quality Rust article
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-blog.