错误代码 E0449

在不允许使用可见性限定符的位置使用了可见性限定符。可见性限定符不允许用于枚举变体、trait 条目、impl 块和 extern 块,因为它们已经继承了父项的可见性。

错误代码示例

#![allow(unused)]
fn main() {
struct Bar;

trait Foo {
    fn foo();
}

enum Baz {
    pub Qux, // error: visibility qualifiers are not permitted here
}

pub impl Bar {} // error: visibility qualifiers are not permitted here

pub impl Foo for Bar { // error: visibility qualifiers are not permitted here
    pub fn foo() {} // error: visibility qualifiers are not permitted here
}
}

要修复此错误,只需移除可见性限定符。 示例

#![allow(unused)]
fn main() {
struct Bar;

trait Foo {
    fn foo();
}

enum Baz {
    // Enum variants share the visibility of the enum they are in, so
    // `pub` is not allowed here
    Qux,
}

// Directly implemented methods share the visibility of the type itself,
// so `pub` is not allowed here
impl Bar {}

// Trait methods share the visibility of the trait, so `pub` is not
// allowed in either case
impl Foo for Bar {
    fn foo() {}
}
}