Skip to content

Universal function call syntax for Vec::contains does not work. #100227

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

Closed
porky11 opened this issue Aug 7, 2022 · 6 comments
Closed

Universal function call syntax for Vec::contains does not work. #100227

porky11 opened this issue Aug 7, 2022 · 6 comments
Labels
C-bug Category: This is a bug.

Comments

@porky11
Copy link

porky11 commented Aug 7, 2022

fn test() {
    let vec = Vec::new();
    vec.contains(&0); // works
    Vec::contains(&vec, &0); // doesn't work
}

Meta

rustc --version --verbose:

rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: x86_64-unknown-linux-gnu
release: 1.62.1
LLVM version: 14.0.5
@porky11 porky11 added the C-bug Category: This is a bug. label Aug 7, 2022
@ghost
Copy link

ghost commented Aug 7, 2022

Can you elaborate on how it doesn't work?

@Noratrieb
Copy link
Member

contains is not a method on Vec<T>, but on slices. You can use method call syntax here because . goes through Deref, and Vec<T> derefs to &[T].
https://doc.rust-lang.org/std/primitive.slice.html#method.contains

@Noratrieb
Copy link
Member

error[E0599]: no function or associated item named `contains` found for struct `Vec<_, _>` in the current scope
 --> src/lib.rs:4:10
  |
4 |     Vec::contains(&vec, &0); // doesn't work
  |          ^^^^^^^^ function or associated item not found in `Vec<_, _>`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

But the error is definitely not good here and could be improved to say there the methods does exist on something that the type derefs to.

@porky11
Copy link
Author

porky11 commented Aug 7, 2022

contains is not a method on Vec<T>, but on slices.

You are right, I've been stupid

@porky11 porky11 closed this as completed Aug 7, 2022
@Noratrieb
Copy link
Member

Noratrieb commented Aug 7, 2022

Would you like opening an issue for the diagnostic? A hint telling that the method exists on a deref would be quite nice.

@Noratrieb
Copy link
Member

I've opened an issue.

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.
Projects
None yet
Development

No branches or pull requests

2 participants