E0050

未匹配的标注

试图实现的 trait 方法的函数参数数量错误。

错误代码示例:


trait Foo {
    fn foo(&self, x: u8) -> bool;
}

struct Bar;

// error: method `foo` has 1 parameter but the declaration in trait `Foo::foo`
// has 2
impl Foo for Bar {
    fn foo(&self) -> bool { true }
}

例如, Foo trait 具有一个方法 foo,有两个函数参数(&selfu8), 但是实现了 fooBar 类型忽略了 u8 参数。要修复此问题,它们得具有相同的参数:

trait Foo {
    fn foo(&self, x: u8) -> bool;
}

struct Bar;

impl Foo for Bar {
    fn foo(&self, x: u8) -> bool { // 没问题!
        true
    }
}

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/rust-rcei-2020/...

译文地址:https://learnku.com/docs/rust-rcei-2020/...

上一篇 下一篇
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~