Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec<T> implements PartialEq<[T; N]> but the other way round doesn't work #125243

Open
Ablesius opened this issue May 18, 2024 · 1 comment
Open
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Ablesius
Copy link

Hello,

it seems that there is an oversight (or possibly it's wanted behaviour, but it felt odd to me) in how vectors and arrays can be compared, based on PartialEq implementation.

I tried this code:

fn main() {
    let a = vec![1, 2, 3];
    let b = [1, 2, 3];
    
    assert_eq!(a, b);
    assert_eq!(b, a);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3c95d140909d5ac62d769b8f663bcd47

I expected to see this happen: No errors because assert_eq should consider these two equal (or shouldn't it?)

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `[{integer}; 3]` with `Vec<{integer}>`
 --> src/main.rs:6:5
  |
6 |     assert_eq!(b, a);
  |     ^^^^^^^^^^^^^^^^ no implementation for `[{integer}; 3] == Vec<{integer}>`
  |
  = help: the trait `PartialEq<Vec<{integer}>>` is not implemented for `[{integer}; 3]`
  = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider dereferencing here
 --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:40:35
  |
40|                 if !(*left_val == **right_val) {
  |                                   +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Meta

I also checked beta and nightly (on the playground, not on my own machine; please tell me if I should repeat it here), and the results are the same over all three of these.

rustc --version --verbose:

rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-unknown-linux-gnu
release: 1.77.2
LLVM version: 17.0.6

Backtrace

(I did activate RUST_BACKTRACE=1 but the output was the same as before with the other runs)

@Ablesius Ablesius added the C-bug Category: This is a bug. label May 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 18, 2024
@Ablesius
Copy link
Author

Update:

let slice: &[i32] = &[1, 2, 3];
let vec = vec![1, 2, 3];

assert_eq!(slice, vec);
assert_eq!(vec, slice);

this works, which indicates further that it might just be an oversight that it doesn't work for comparing an array with a vec.

@saethlin saethlin added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants