Open
Description
I expected that these invocations of sort_by
and sort_by_key
would be equivalent:
struct Client(String);
impl Client {
fn key(&self) -> &str {
&self.0
}
}
fn main() {
let mut clients: Vec<Client> = vec![];
// Error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
clients.sort_by_key(|c| c.key());
// OK
clients.sort_by(|a, b| a.key().cmp(&b.key()));
}
The implementation of sort_by_key
:
pub fn sort_by_key<B, F>(&mut self, mut f: F)
where F: FnMut(&T) -> B, B: Ord
{
self.sort_by(|a, b| f(a).cmp(&f(b)))
}
An initial attempt at using HRTB didn't seem to pan out:
pub fn sort_by_key<B, F>(&mut self, mut f: F)
where for <'a> F: FnMut(&'a T) -> B + 'a,
B: Ord