I'm trying to sort a Vec<String>
using a key function that returns references to the strings in the vector. A contrived example is to use the identity function as key function (which of course is useless, but it's the minimal example to reproduce my problem):
fn key(x: &String) -> &String {
x
}
Now given items: Vec<String>
, I'd like to be able to do
items.sort_by_key(key);
This gives the following error:
error[E0271]: type mismatch resolving `for<'r> <fn(&std::string::String) -> &std::string::String {main::key} as std::ops::FnOnce<(&'r std::string::String,)>>::Output == _`
--> src/main.rs:19:11
|
19 | items.sort_by_key(key);
| ^^^^^^^^^^^ expected bound lifetime parameter, found concrete lifetime
|
= note: concrete lifetime that was found is lifetime '_#16r
I don't understand why I get this error, so I tried to track this down. I first implemented my own version of sort_by_key()
:
fn sort_by_key<T, K: Ord>(a: &mut [T], key: fn(&T) -> K) {
a.sort_by(|x, y| key(x).cmp(&key(y)));
}
When trying to call this function, I get what looks like the "opposite" error:
error[E0308]: mismatched types
--> src/main.rs:22:29
|
22 | sort_by_key(&mut items, key);
| ^^^ expected concrete lifetime, found bound lifetime parameter
|
= note: expected type `fn(&std::string::String) -> _`
found type `fn(&std::string::String) -> &std::string::String {main::key}`
I can make this code compile by fixing the key type to &T
instead of using the generic parameter K
, or by using &K
instead of K
as return type for the key function:
fn sort_by_key_v2<T: Ord>(a: &mut [T], key: fn(&T) -> &T) {
a.sort_by(|x, y| key(x).cmp(&key(y)));
}
fn sort_by_key_v3<T, K: Ord>(a: &mut [T], key: fn(&T) -> &K) {
a.sort_by(|x, y| key(x).cmp(&key(y)));
}
I also tried adding lifetime annotations, but that only shifted the error around without resolving it.
Here's the three versions of the sort_by_key()
function on the Playground.
Why am I getting these errors? Is there any way to fix them while keeping the key type K
completely generic?
See Question&Answers more detail:
os