Это объявление и определение черты работают правильно без каких-либо проблем:
trait FTrait<T>: Fn(T, T) -> T {}
impl<T, F> FTrait<T> for F where F: Fn(T, T) -> T, {}
...
fn hof(f: impl FTrait<u32>) -> impl FTrait<u32> { //fourth with a generic trait in use with concrete types
move |a, b| {
let r = f(a, b);
r
}
}
Но это объявление черты дает несколько ошибок:
trait FTraitBorrowed<'a, T>: Fn(&'a T, &'a T) -> &'a T {}
impl<'a, T, F> FTraitBorrowed<'a, T: 'a, F> for F where F: Fn(&'a T, &T) -> &'a T, {}
....
fn hof_borrowed(f: impl FTraitBorrowed<i32>) -> impl FTraitBorrowed<i32 > {
move |a, b| {
let r = f(a, b);
r
}
Ошибки перечислены здесь:
Errors:
error: associated type bindings must be declared after generic parameters
--> src\main.rs:44:31
|
44 | impl<'a, T, F> FTraitBorrowed<'a, T: 'a, F> for F where F: Fn(&'a T, &T) -> &'a T, {}
| ^^^^-----^^^
| |
| this associated type binding should be moved after the generic parameters
error[E0658]: associated type bounds are unstable
--> src\main.rs:44:35
|
44 | impl<'a, T, F> FTraitBorrowed<'a, T: 'a, F> for F where F: Fn(&'a T, &T) -> &'a T, {}
| ^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/52662
error[E0229]: associated type bindings are not allowed here
--> src\main.rs:44:35
|
44 | impl<'a, T, F> FTraitBorrowed<'a, T: 'a, F> for F where F: Fn(&'a T, &T) -> &'a T, {}
| ^^^^^ associated type not allowed here
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0229, E0658.
For more information about an error, try `rustc --explain E0229`.
Не могу четко понять, что не так из подсказок.
Поскольку первая ошибка исчезнет, если я поменяю местами F и T, как это в реализации:
impl<'a, T, F> FTraitBorrowed<'a, F, T: 'a, > ...
Может кто-нибудь помочь, пожалуйста?
Спасибо.