Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upbinary_search methods should use Borrow #32822
Comments
huonw
added
the
A-libs
label
Apr 8, 2016
This comment has been minimized.
This comment has been minimized.
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 |
huonw
added
I-nominated
T-libs
labels
Apr 8, 2016
This comment has been minimized.
This comment has been minimized.
|
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.) |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
|
Ah, ok, thanks for scaling back the hyperbole ;) You are correct that |
This comment has been minimized.
This comment has been minimized.
|
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 |
alexcrichton
added
P-medium
and removed
I-nominated
labels
May 4, 2016
brson
added
the
E-help-wanted
label
Nov 3, 2016
This comment has been minimized.
This comment has been minimized.
|
All we need to do here is the survey @alexcrichton suggested, then make the patch. |
This comment has been minimized.
This comment has been minimized.
|
I just looked over the standard library and the following are candidates for this change: Slice
CloneOrd, PartialOrd and PartialEqThe LinkedListVecDequeue |
This comment has been minimized.
This comment has been minimized.
|
Turns out |
christophebiocca
referenced this issue
Nov 14, 2016
Merged
Use Borrow for binary_search and contains methods in the standard library #37761
bors
added a commit
that referenced
this issue
Dec 20, 2016
alexcrichton
added a commit
to alexcrichton/rust
that referenced
this issue
Dec 20, 2016
This comment has been minimized.
This comment has been minimized.
|
Added in #37761, so closing. |
alexcrichton
closed this
Jan 1, 2017
This comment has been minimized.
This comment has been minimized.
|
#37761 Seem to have only added the change to core, not to std. Was this intentional? |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Yeah, exactly. |
osa1 commentedApr 8, 2016
binary_search()takes a reference of the element instead of usingBorrowinstances. This is causing problems:&Stringdoesn't make any sense. I should be able to just use&swheresis aStringto search in aVec<String>.&strat hand, I need to allocate aString)Borrowfor this purpose. E.g.HashMap. As a guideline, I think lookup functions of containers should just useBorrowalways.