E0412 Cannot Find Type Fix
Written November 27, 2026 · Tested with Rust 1.99.1 · 23 min read
- The error: a type name that is not in this scope
- First fix: declare the type
- The same header in the other type positions
- The spelling form: a similar name exists
Written November 27, 2026
Tested with Rust 1.99.1
What this guide is
Search for E0412 and the compiler disagrees about the number. E0412 was the code for a type name that is not in scope — a type used in a function argument, a return type, a field, or an annotation that has not been declared, imported, or spelled the way the type it means is spelled. rustc --explain E0412 opens with the note that this error code is no longer emitted by the compiler; the official page for E0412 itself carries no such note. The fault itself has not gone away: on the current compiler the same code prints E0425, with the header "cannot find type" followed by the name and "in this scope". Anyone following an older answer, an older book, or the E0412 page itself will meet the fault under its new number, and this guide is written for that meeting: it keeps E0412 in the title because that is the search that leads here, and it shows every diagnostic under the number the current compiler actually prints.
The fault has only a handful of causes, and the diagnostic usually names which one. The type was never declared. The name is a misspelling of a type that exists, and the compiler often points at the similar name. The type exists in the standard library or in another module, but nothing has brought it into this scope — the prelude covers only the most common standard types, so a name such as HashMap needs an import or a full path. The type was imported in a parent module, which does not make it visible inside a child module. Or the name was meant to be a generic parameter of the function itself, and the parameter was never declared in angle brackets. The sections below take the shapes in order, then separate the neighboring errors: an unknown value, which is also E0425 but reads "cannot find value"; an unknown struct in a struct literal, which is E0422; an unresolved import, which is E0432; and an unknown name in trait position, which is E0405.
The error: a type name that is not in this scope
A function argument annotated with a type that has not been declared anywhere fails before the function body is considered:
fn show(w: Widget) {
println!("widget");
}
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `Widget` in this scope
--> main.rs:1:12
|
1 | fn show(w: Widget) {
| ^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
Read the header literally: "cannot find type Widget in this scope". The word "type" is the position the name was used in — the compiler resolved the name in the type namespace, found nothing there under that spelling, in that scope, and underlined the one occurrence. The note under the span, "not found in this scope", carries the two questions that decide the repair: does a type of this name exist somewhere, and if it does, is that somewhere this scope? If it exists nowhere, it has to be declared. If it exists elsewhere — the standard library, another module, a parent module — it has to be imported or reached by a path. If the spelling is wrong, neither declaring nor importing helps until the spelling is corrected.
First fix: declare the type
If the type is the program's own and it simply has not been written yet, declaring it is the whole repair:
#[derive(Debug)]
struct Widget;
fn show(w: Widget) {
println!("{:?}", w);
}
fn main() {
show(Widget);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
Widget
The program prints Widget and exits with status 0. A declaration in the same module puts the name in scope for every use in that module — arguments, return types, fields, and annotations alike. Where the declaration belongs in another module, declaring it again locally is the wrong repair: a second, distinct type with the same name compiles at this use and then fails to match the original type everywhere the two meet, and the module sections below are the repair instead.
The same header in the other type positions
The position changes nothing about the code or the header. A struct field of an undeclared type fails identically:
struct Holder {
item: Widget,
}
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `Widget` in this scope
--> main.rs:2:11
|
2 | item: Widget,
| ^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
And so does a generic argument — the element type of a Vec in a variable annotation:
fn main() {
let v: Vec<Widget> = Vec::new();
println!("{}", v.len());
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `Widget` in this scope
--> main.rs:2:16
|
2 | let v: Vec<Widget> = Vec::new();
| ^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
The same printing, with the span moved, covers the remaining positions: a function return type (fn make() -> Widget), an impl block for an undeclared type (impl Something {} — the first erroneous example on the official E0412 page), and a trait method argument of an undeclared type (fn bar(x: N); inside a trait). In each case the underline sits on the type name itself, the header says "cannot find type", and the repair is decided by the same two questions — declared somewhere, and in scope here. One position is different, and it matters: a name used where a trait is expected — a bound such as T: Missing, an impl Missing argument, a dyn Missing — is not this error at all. It prints E0405, "cannot find trait", because bounds resolve in the trait namespace rather than the type namespace.
The spelling form: a similar name exists
A misspelled standard type produces the same header, with an addition that names the repair. Sting for String:
fn main() {
let s: Sting = String::from("hi");
println!("{}", s);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `Sting` in this scope
--> main.rs:2:12
|
2 | let s: Sting = String::from("hi");
| ^^^^^
|
::: /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:353:1
|
353 | pub struct String {
| ----------------- similarly named struct `String` defined here
|
help: a struct with a similar name exists
|
2 | let s: String = String::from("hi");
| +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
The second span is the tell: the compiler points into the standard library at the similarly named struct and offers the corrected line as a help, "a struct with a similar name exists". A coined name such as Vek for Vec prints the same header with the same kind of similar-name help. When that help appears, take it before considering any other repair — declaring a new type named Sting would also silence this one diagnostic, by creating a second type that is not the one the rest of the line already uses. The corrected program:
fn main() {
let s: String = String::from("hi");
println!("{}", s);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
hi
The program prints hi and exits with status 0. String needs no import here because it is in the prelude — the set of names every Rust program starts with in scope. That is exactly what separates this form from the next one: a standard type outside the prelude is spelled correctly and still not found.
The missing-import form: a standard type outside the prelude
HashMap is spelled correctly and exists in the standard library, but it is not in the prelude, so using its bare name without an import fails — twice in one line, under two different codes:
fn main() {
let mut m: HashMap<String, i32> = HashMap::new();
m.insert(String::from("a"), 1);
println!("{}", m.len());
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `HashMap` in this scope
--> main.rs:2:16
|
2 | let mut m: HashMap<String, i32> = HashMap::new();
| ^^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 + use std::collections::HashMap;
|
error[E0433]: cannot find type `HashMap` in this scope
--> main.rs:2:39
|
2 | let mut m: HashMap<String, i32> = HashMap::new();
| ^^^^^^^ use of undeclared type `HashMap`
|
help: consider importing this struct
|
1 + use std::collections::HashMap;
|
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0425, E0433.
For more information about an error, try `rustc --explain E0425`.
Both errors carry the same help — "consider importing this struct", with the exact use line drawn in — and one import repairs both. The split between them is position again: the annotation occurrence, in type position, is the E0425 of this guide; the constructor occurrence, HashMap::new(), is reported as E0433, "use of undeclared type". Do not read the second code as a second fault. Taking the help is the fix:
use std::collections::HashMap;
fn main() {
let mut m: HashMap<String, i32> = HashMap::new();
m.insert(String::from("a"), 1);
println!("{}", m.len());
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
1
The program prints 1 and exits with status 0. The alternative repair, for a name used once, is the full path at the use itself — std::collections::HashMap<String, i32> and std::collections::HashMap::new() compile and run with no import at all. The import is the better default when the name recurs; the path is the better default when it does not, because an unused import is its own warning. The same pair of repairs covers every standard type outside the prelude — HashSet, BTreeMap, PathBuf, File — and every type from a crate the program depends on.
The module form: the type exists, in another module
A type defined in a sibling module is not visible by its bare name outside that module, even in the same file:
mod shapes {
pub struct Widget;
}
fn show(w: Widget) {
println!("widget");
}
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `Widget` in this scope
--> main.rs:5:12
|
5 | fn show(w: Widget) {
| ^^^^^^ not found in this scope
|
help: consider importing this struct
|
1 + use crate::shapes::Widget;
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
The help again names the repair, this time with the path through the defining module: "consider importing this struct", use crate::shapes::Widget;. Note what made the import possible at all — the struct is declared pub. A private item in another module cannot be imported from here, and attempting it is a privacy error rather than this one; visibility is decided in the defining module. Either repair works — the import the help offers, or the path written at each use:
mod shapes {
#[derive(Debug)]
pub struct Widget;
}
fn show(w: shapes::Widget) {
println!("{:?}", w);
}
fn main() {
show(shapes::Widget);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
Widget
The program prints Widget and exits with status 0. Writing the import instead of the path — use shapes::Widget; at the top, then the bare name everywhere — compiles and runs identically. Choose the import when the name recurs in the file, the path when it appears once, exactly as with the standard-library form.
The parent-module form: an import does not reach into a child module
The case the official E0412 page singles out is subtler: a type imported at the top of a file is in scope at the top of the file, and not inside a module defined in that file. Modules have their own scopes, and a child module does not inherit its parent's imports:
use std::fs::File;
mod foo {
fn some_function(f: File) {}
}
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `File` in this scope
--> main.rs:4:25
|
4 | fn some_function(f: File) {}
| ^^^^ not found in this scope
|
help: consider importing one of these structs
|
4 + use crate::File;
|
4 + use std::fs::File;
|
warning: unused import: `std::fs::File`
--> main.rs:1:5
|
1 | use std::fs::File;
| ^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0425`.
Every part of this printing teaches the rule. The help offers two imports, both placed inside the module — "use crate::File", reaching the parent's import, or "use std::fs::File", importing the original directly. And the warning that follows is the same fact seen from the other side: the top-level import is reported unused precisely because the only use of the name is inside the module, where that import does not reach. The official page's stated repair is use super::File; inside the child module, importing the type from the parent namespace:
use std::fs::File;
mod foo {
use super::File;
pub fn some_function(f: File) -> u64 {
f.metadata().map(|m| m.len()).unwrap_or(0)
}
}
fn main() {
let f = File::create("data.txt").unwrap();
println!("{}", foo::some_function(f));
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
0
The program prints 0 — the length of the file it just created — and exits with status 0. super in a path resolves to the parent module, so super::File names the parent's import from inside the child. Importing std::fs::File directly inside the module, as the second help line offers, compiles and runs the same way.
The generic form: the parameter was never declared
A single capital letter used as a type is usually not a missing type at all — it is a generic parameter the author meant to declare. The official E0412 page's third erroneous example is exactly this shape:
fn foo(x: T) {
println!("hi");
}
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find type `T` in this scope
--> main.rs:1:11
|
1 | fn foo(x: T) {
| ^ not found in this scope
|
help: you might be missing a type parameter
|
1 | fn foo<T>(x: T) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
The help is distinct from every other form in this guide: "you might be missing a type parameter", with the declaration <T> drawn into the function header. A generic parameter is a type that is in scope only because the item declares it — there is nothing to import and nothing else to declare. Taking the help, with a bound so the body can print the value:
fn foo<T: std::fmt::Display>(x: T) {
println!("{}", x);
}
fn main() {
foo(42);
}
$ rustc --edition 2021 main.rs -o main.bin
$ ./main.bin
42
The program prints 42 and exits with status 0. The trait-method form on the official page is the same fault inside a trait: fn bar(N); declares a method argument of an undeclared type, and the page's repair is an associated type — type N; in the trait, with the argument written Self::N. In both places the pattern is the same: a name that was meant to stand for a type the item itself supplies has to be declared by that item, in its parameter list or its associated types.
Neighboring error: an unknown value is also E0425
The closest look-alike shares the code. A name used as a value, rather than as a type, that is not in scope prints the same E0425 with one word changed:
fn main() {
let x = unknown_variable;
println!("{}", x);
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0425]: cannot find value `unknown_variable` in this scope
--> main.rs:2:13
|
2 | let x = unknown_variable;
| ^^^^^^^^^^^^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
"cannot find value" against this guide's "cannot find type" — that one word is the whole split, and it follows the position of the use, not the intent behind it. The same unknown word prints "cannot find type" in an annotation and "cannot find value" in an expression. So the code alone does not tell you which fault you have; the header's noun does. The repairs overlap — check the spelling, declare or import the name — but they are applied in different namespaces: a variable is declared with let, a function is imported by its path, and neither repair puts a name into the type namespace. Older material that separates these by code, E0412 for types against E0425 for values, describes a split the current compiler no longer makes.
Neighboring error: an unknown struct in a literal is E0422
Using an unknown name as a struct in a struct literal — name followed by fields in braces — is a different code, because the use claims more than a type: it claims a struct with those fields:
fn main() {
let x = Foo { x: 1, y: 2 };
println!("done");
}
$ rustc --edition 2021 main.rs -o main.bin
error[E0422]: cannot find struct, variant or union type `Foo` in this scope
--> main.rs:2:13
|
2 | let x = Foo { x: 1, y: 2 };
| ^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0422`.
The header names all three things a literal of that shape could construct: "cannot find struct, variant or union type". The same unknown name in a plain annotation in the same program would be this guide's E0425 printing instead — the literal syntax is what moves the code. The repairs are the familiar ones, declare the struct or import it, but the code tells you the use site expected a struct-shaped type specifically, which an annotation never does.
Neighboring error: an unresolved import is E0432
When the failing line is itself a use statement, the code is neither of the above. The import is the statement that failed to resolve:
use something::Foo;
fn main() {}
$ rustc --edition 2021 main.rs -o main.bin
error[E0432]: unresolved import `something`
--> main.rs:1:5
|
1 | use something::Foo;
| ^^^^^^^^^ use of unresolved module or unlinked crate `something`
|
= help: you might be missing a crate named `something`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0432`.
The header says "unresolved import", the span covers the whole import path, and the note names the first segment as an unresolved module or unlinked crate. That is a different fault from everything above: here the name being imported may be spelled perfectly, and the failure is in the path the import follows — a module that does not exist, a crate that is not a dependency, or a segment that is not exported. The boundary with the HashMap form above is exact: a missing import for an existing type is repaired by adding a use line and prints E0425 at the use; a use line whose path does not resolve prints E0432 at the import itself. One related code appeared in that HashMap printing: E0433, on the constructor occurrence of an undeclared type — a path use of a name that was never imported, where E0432 is an import statement that never resolved.
The exact rule: a type name must resolve in the type namespace of its scope
The official page for E0412 defines the historical fault through its examples — impl Something {} for an undeclared type, a trait method argument of an undeclared type, and fn foo(x: T) for an undeclared parameter — and states the fix in one sentence: verify you did not misspell the type name, and that you did declare it or import it into the scope. The note in rustc --explain E0412 states that the code is no longer emitted, and the current compiler makes the same diagnosis as E0425, whose official page describes an unresolved name being used and likewise directs the reader to check the spelling and to import the item with a use statement when it is not defined in the current module.
The mechanism is name resolution by scope and namespace. A type name in an annotation, a field, a return type, or a generic argument is resolved in the type namespace of the scope it appears in, and a scope contains only what was declared in it, imported into it, or supplied by the prelude — the Reference's page on paths describes how a path's segments each resolve to an item, with self, super, and crate resolving relative to the current module, the parent module, and the crate root respectively, which is why those three prefixes are the tools in the module repairs above. Imports are per-scope, not per-file: the Book's chapter on modules states that within a scope, the use keyword creates shortcuts to items, and that code within a module is private from its parent modules by default — the two facts behind the module form and the parent-module form respectively. Nothing about the fault is a type mismatch: the compiler is not comparing this type against an expected one, it is reporting that the name denotes no type at all in the place it was written.
The fixes, ranked
- First, read the help. In most forms the compiler states the repair outright — a similar name for a misspelling, the exact
useline for a missing import or a sibling module, the<T>declaration for a missing parameter — and taking the offered line is faster and more reliable than deriving the same repair. - Second, correct the spelling, when a similar-name help appears. The type already exists and the rest of the code already refers to it by its real name.
- Third, import the type, or write its full path at the use, when the type exists in the standard library, a dependency, or another module and the spelling is right. Import when the name recurs; use the path when it appears once.
- Fourth, declare the type, when it is the program's own and exists nowhere yet — in the module where its uses live, or in its own module with
puband an import at each use. - Fifth, declare the parameter, when the name was meant to be generic:
<T>in the item's parameter list, or an associated type in a trait. There is nothing to import because the name stands for a type supplied at each call.
Which fix for which cause
- A function argument, return type, struct field, variable annotation, or generic argument names an undeclared type: E0425 on the current compiler, historically E0412, header "cannot find type", span on the name, no help or a plain not-found note. Declare the type in this module.
- The same header, with a second span in the standard library labeled "similarly named struct" and a help offering the corrected line: a misspelling. Correct the spelling to the offered name.
- A correctly spelled standard type such as
HashMapfails, with a help offering auseline, and a second error E0433 on the constructor occurrence in the same line: a missing import for a type outside the prelude. Add the offered import, or write the full path at each use. - A type defined in a sibling module fails by its bare name, with a help offering
use crate::and that module's path: a module-scope fault. Add the import or write the module path at the use — and check the item ispubin its module. - A type imported at the top of a file fails inside a module defined in that file, with the top-level import also reported unused: imports do not reach into child modules. Import inside the module —
use super::for the parent's import, or the original path directly. - A single-letter or placeholder name fails in a function or trait method, with the help "you might be missing a type parameter": an undeclared generic. Declare
<T>on the function, or an associated type in the trait. - An unknown name in an expression position: E0425 again, but the header reads "cannot find value". Declare the variable or import the function; the code is shared and the noun in the header is the split.
- An unknown name heading a struct literal with fields: E0422, "cannot find struct, variant or union type". Declare or import the struct.
- The failing line is a
usestatement itself: E0432, "unresolved import". Fix the import's path, the missing dependency, or the unexported segment — not the use sites. - An unknown name in a bound, an
impl Traitargument, or adyntype: E0405, "cannot find trait". Declare or import the trait; trait names resolve in the trait namespace, not the type namespace.
Checklist
- Read the code the compiler printed, not the code older material cites. A "cannot find type" fault is E0412 in older sources and E0425 from the current compiler; the header's words, not the number, identify the fault.
- Read the noun in the header. "cannot find type", "cannot find value", "cannot find trait", and "cannot find struct, variant or union type" are four different resolutions of an unknown name, decided by the position of the use.
- Take the help when one is offered. A similar name, an exact
useline, and a drawn-in<T>are each the compiler stating the repair it has already verified applies. - Ask the two scope questions before editing. Does this type exist anywhere — and if it does, is it in scope here? The first answer separates declaring from importing; the second separates a missing import from a wrong path.
- Remember that imports are per module. An import at the top of a file covers that module only; a child module needs its own import, and
super::reaches the parent's names. - Fix the import before the use sites. An E0432 on the
useline, or an E0433 on a constructor, is upstream of the annotations that name the same type — repair the resolution once and recompile before reading the rest.
Related guides on this site
- Cannot Transmute Zero-Sized Type E0591 Fix
- E0405 Cannot Find Trait Fix
- E0422 Cannot Find Struct Fix
- E0509 Cannot Move Out Fix
Sources
- Rust Compiler Error Index, E0412 — for the erroneous examples of an
implfor an undeclared type, a trait method argument of an undeclared type, and a function argument of an undeclared generic parameter, for verifying the spelling and that the type was declared or imported into the scope, and for the parent-module case repaired withuse super::File;. - Rust Compiler Error Index, E0425 — for an unresolved name being used, for verifying the name was not misspelled, and for importing an item that is not defined in the current module with a
usestatement. - The Rust Reference, Paths — for a path being a sequence of segments that refer to items, types, and other names, and for
self,super, andcrateresolving relative to the current module, the parent module, and the crate root. - The Rust Programming Language, Defining Modules to Control Scope and Privacy — for the
usekeyword creating shortcuts to items within a scope, and for code within a module being private from its parent modules by default. - Rust Compiler Error Index, E0422 — for an identifier that is neither defined nor a struct being used as a struct.
- Rust Compiler Error Index, E0432 — for an import being unresolved, and for verifying the import name was not misspelled and exists in the module it was imported from.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.