-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
binary_search methods should use Borrow #32822
Comments
Hm, could you clarify this point? fn main() {
let v = vec!["abc".to_string(), "def".to_string()];
let s: String = "def".to_string();
println!("{:?}", v.binary_search(&s));
} Also, it's worth noting that fn main() {
let v = vec!["abc".to_string(), "def".to_string()];
let s: &str = "def";
println!("{:?}", v.binary_search_by(|x| x.as_str().cmp(s)));
} (Don't get me wrong, it would be nicer if |
Nominating for libs team discussion: we should be able to relax the parameter here, but seems worth discussing. (It is formally a minor change, and I suspect that not many people are relying on this function for type inference.) |
I didn't mean it doesn't work or anything like that. I just meant that if I want to pass a read-only reference to a string I'd use |
Ah, ok, thanks for scaling back the hyperbole ;) You are correct that |
The libs team discussed this issue during triage today and the conclusion was that we probably want to do this as it basically means "more code works" at only the slight cost of readability in the docs. Before this is done, however, we'd like to see a bit of a small survey of the rest of the standard library that this pattern could also apply. For example the |
All we need to do here is the survey @alexcrichton suggested, then make the patch. |
I just looked over the standard library and the following are candidates for this change: Slice
CloneOrd, PartialOrd and PartialEqThe LinkedListVecDequeue |
Turns out |
…efactor, r=alexcrichton Use Borrow for binary_search and contains methods in the standard library Fixes all standard library methods in rust-lang#32822 that can be fixed without backwards compatibility issues.
Added in #37761, so closing. |
#37761 Seem to have only added the change to core, not to std. Was this intentional? |
|
Yeah, exactly. |
binary_search()
takes a reference of the element instead of usingBorrow
instances. This is causing problems:&String
doesn't make any sense. I should be able to just use&s
wheres
is aString
to search in aVec<String>
.&str
at hand, I need to allocate aString
)Borrow
for this purpose. E.g.HashMap
. As a guideline, I think lookup functions of containers should just useBorrow
always.The text was updated successfully, but these errors were encountered: