3 ms·
I was surprised that I was OK with that fact that in Rust a vector and an array with the same elements compare equal. It felt completely reasonable, and I defin
by tialaramex 5y ago
I was surprised that I was OK with that fact that in Rust a vector and an array with the same elements compare equal. It felt completely reasonable, and I definitely didn't feel that way about it in some other languages where that's true.
- bsder 5y agoErm, no, that doesn't work. I created a quick check in the Rust Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=473a0c6db6ca709a11dd501e38f3f0dc https://play.rust-lang.org/?version=stable&mode=debug&editio... Do you have some more context? I suspect somehow things are getting coerced to slices and then compared.
- Measter 5y agoThe implementation isn't symmetric, it'll work if you compare `b == a`. Apparently[0] it's something to do with avoiding code bloat. [0] https://github.com/rust-lang/rust/blob/e4c23daeb461ac02413eb36c8cefcc5530638a05/library/alloc/src/vec/partial_eq.rs#L39 https://github.com/rust-lang/rust/blob/e4c23daeb461ac02413eb...
- fee1-dead 5y agoI suppose the reason is Vec<T> implements `Borrow<[T]>` and [T; N] implements Borrow<[T]> as well. The trait implementation requires these types to have same implementations for all traits including comparisons and hashing. Returning `false` when types don't match doesn't make sense in Rust since Rust is strongly typed (and not interpreted). And say if we don't make them compare equal. then vec![1, 2] != [1, 2] but &vec![1, 2] == &[1, 2], which is just too opaque and confusing. This behavior is no fit for a language like Rust.