我学到了一个习语,为 Trait 起一个别名。但是,当我将这个习惯用法应用到我的代码中时,我遇到了一个令人费解的错误。
这是代码的简化版本:
// `StrToStr` is an alias for `Fn(&str) -> &str`
trait StrToStr: Fn(&str) -> &str {}
impl<T> StrToStr for T where T: Fn(&str) -> &str {}
// A function that returns `StrToStr`
fn identity() -> impl StrToStr {
|s: &str| s
}
Run Code Online (Sandbox Code Playgroud)
当我编译此代码时,出现以下错误:
error[E0308]: mismatched types
--> src/main.rs:77:18
|
77 | fn identity() -> impl StrToStr {
| ^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected reference `&str`
found reference …Run Code Online (Sandbox Code Playgroud) rust ×1