错误代码 E0412
使用的类型名称不在作用域内。
错误代码示例
#![allow(unused)] fn main() { impl Something {} // error: type name `Something` is not in scope // or: trait Foo { fn bar(N); // error: type name `N` is not in scope } // or: fn foo(x: T) {} // type name `T` is not in scope }
要修复此错误,请检查您是否拼错了类型名称,或者您是否声明了它或将其导入到作用域中。示例
#![allow(unused)] fn main() { struct Something; impl Something {} // ok! // or: trait Foo { type N; fn bar(_: Self::N); // ok! } // or: fn foo<T>(x: T) {} // ok! }
导致此错误的另一种情况是将类型导入到父模块中。 要修复此问题,您可以按照建议直接使用 File 或 use super::File;
,这将从父命名空间导入类型。 以下是一个导致此错误的示例
#![allow(unused)] fn main() { use std::fs::File; mod foo { fn some_function(f: File) {} } }
use std::fs::File; mod foo { // either use super::File; // or // use std::fs::File; fn foo(f: File) {} } fn main() {} // don't insert it for us; that'll break imports