News for hackers. Stuff that matters.
Re:Equality Is Hard by jeffadotio on Monday March 09, 2020 @09:17PM (#22531804)
I really like this about Rust, implementing equality is just another trait. The same is true of comparison operators. If you don’t want a type to be able to be compared by == you can make it always evaluate to false or even panic and give a message that the type cannot be used in that way. That might aggravate coders who do not test comprehensively but it is an option.

Edit: This is a response to the concerns of the article. For general info on this it’s a great introduction to Rust’s awesome documentation.

https://doc.rust-lang.org/std/cmp/trait.Eq.html

Re:Equality Is Hard by tus88 on Monday March 09, 2020 @09:20PM (#22531817)
You mean there is no compile time way to accomplish that? Doesn't sound very Rust-like.
Re:Equality Is Hard by re on Monday March 09, 2020 @11:08PM (#22532411)
It is determined at compile time; I'm not sure why GP is suggesting returning false if you don't want types to be compared. By default structs are not comparable. If you opt-in to the standard PartialEq implementation using the `derive` annotation[1], it will only allow a struct to be compared for equality with other structs of the same type. You can choose to add implementations to compare against other types[2].

[1] https://doc.rust-lang.org/book/ch05-02-example-structs.html#...

[2] https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#how-c...

Re:Equality Is Hard by artursapek on Tuesday March 10, 2020 @12:52AM (#22532834)
Rust is such a delightful language.
Re:Equality Is Hard by jeffadotio on Monday March 09, 2020 @09:29PM (#22531880)
“Option” is the operative word. A type that does not implement the correct trait cannot be used in a way that requires the trait. If you are writing anything non-trivial that would probably be preferable. It requires nothing, that is the default behavior, so I did not bother to mention it.
Re:Equality Is Hard by aliceryhl on Tuesday March 10, 2020 @05:02AM (#22533892)
I don't know why they would make it always return false. In Rust you can just not implement Eq for the type, and it becomes a compile error to try and compare it.