-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add Vec::contains_ref which is worse for inference but less restrictive than Vec::contains #42671
Comments
You could use |
I'm aware of this workaround, but isn't that what "contains" should be in the first place? |
There was an attempt to fix this in #43020 but it has caused some major inference failure. |
Is the alternative mentioned in #43020 (comment) any better with respect to the inference failure? |
Tagging C-feature-accepted to follow up on the approach in #43020 (comment). Please submit a PR with an implementation and tests. We will need to confirm that this causes less breakage than the original |
This allows `contains` to be used on slices to search for elements that satisfy a partial equality with the slice item type, rather than an identity. This closes rust-lang#42671. Note that string literals need to be referenced in this implementation due to `str` not implementing `Sized`. It’s likely this will have to go through a Crater run to test for breakage (see rust-lang#43020).
Generalise slice::contains over PartialEq This allows `contains` to be used on slices to search for elements that satisfy a partial equality with the slice item type, rather than an identity. This closes #42671. Note that string literals need to be referenced in this implementation due to `str` not implementing `Sized`. It’s likely this will have to go through a Crater run to test for breakage (see #43020).
This was attempted using the other approach in #46934. Unfortunately, it too causes too much breakage in downstream crates. Even though this API change would be significantly nicer, it doesn't seem plausible unless this breakage is acceptable (maybe for a later epoch). |
Note that the problems mentioned here are the same as those in #20063. |
Changed from .contains() to .first().unwrap() == in tests because contains is weird rust-lang/rust#42671
Shouldn't this issue and a workaround be mentioned in |
Relabeling as a documentation issue. |
I'm not sure when it was added, but the docs do mention the workaround. Perhaps this issue should be closed? |
How about adding a new method alongside Something like |
I am willing to do the work about |
Can someone review this? |
We just stumbled upon this problem in our project, which is kind of frustrating and makes the code ugly and less readable. Could the Rust edition mechanism (or similar) be used for doing some cleanup in the standard library? I understand that fully supporting programs using two different stdlib editions might requires having two versions of the standard library in memory sharing data together, but in cases of enhancements like this there might be a way for third-party packages to be tested against new versions and declare compatibility with these versions (a program could then be compiled with a newer version only if all its dependencies declare compatibility with that version), progressively allowing the ecosystem to use the better interfaces. |
Any update ? |
If someone wants a |
Using Vec::contains currently only works if the contains parameter is of the same &Type as the Vector elements. This means the parameter must be converted to the vec-item-type even though it is comparable with the type in the first place.
e.g. following code does not work, even though
impl<'a, 'b> PartialEq<str> for String
exists:The Error:
I'd be better if Vec::contains had definition like:
fn contains<U>(&self, value: &U) where T: PartialEq<U>, U: ?Sized
The text was updated successfully, but these errors were encountered: