相关疑难解决方法(0)

预期的类型参数,找到u8,但类型参数是u8

trait Foo {
    fn foo<T>(&self) -> T;
}

struct Bar {
    b: u8,
}

impl Foo for Bar {
    fn foo<u8>(&self) -> u8 {
        self.b
    }
}

fn main() {
    let bar = Bar {
        b: 2,
    };
    println!("{:?}", bar.foo());
}
Run Code Online (Sandbox Code Playgroud)

(游乐场)

上面的代码导致以下错误:

error[E0308]: mismatched types
  --> <anon>:11:9
   |
11 |         self.b
   |         ^^^^^^ expected type parameter, found u8
   |
   = note: expected type `u8` (type parameter)
              found type `u8` (u8)
Run Code Online (Sandbox Code Playgroud)

我的猜测是,问题来自特质中的泛型函数.

types type-inference compiler-errors rust

5
推荐指数
1
解决办法
1262
查看次数

标签 统计

compiler-errors ×1

rust ×1

type-inference ×1

types ×1