Rust
E0422 Cannot Find Struct Fix
You write a struct literal — a name, curly brackets, field values — and the compiler answers that it cannot find that struct in this scope. E0422 means the name in front of the braces resolved to nothing the compiler can build a value from: the struct was never defined, it lives in a module that was never imported, or the name is a near-miss spelling of a struct that does exist. Each cause has a short repair, and the diagnostic itself tells you which one applies — including, for a misspelling, the correct name printed as a suggestion.
Written November 26, 2026 · Tested with Rust 1.99.1 · 11 min read
- The error: a struct literal with no struct behind it
- The fix: define the struct
- The fix: import the struct from its module
- The fix: correct a misspelled name
Written November 26, 2026
Tested with Rust 1.99.1
What this guide is
A struct expression builds a value by naming a struct and filling in its fields inside curly brackets. That form puts the name in a specific position: the compiler must resolve it as a struct, an enum variant, or a union — something that can be constructed that way. When no such item is in scope under that name, the report is E0422, "cannot find struct, variant or union type in this scope". The error codes index defines the code the same way: an identifier that is neither defined nor a struct was used.
Position is what separates E0422 from its neighbors, and the same missing name lands on a different code in each other position. The name in a type annotation or a parameter type is E0425, "cannot find type". The name at the head of a path such as Point::origin() is E0433, also worded "cannot find type" but labeled "use of undeclared type". A name that does resolve — to a local variable — is E0574, with a note saying a local variable of that name exists in another namespace. E0422 is only the struct-literal position: name, then braces. That precision is useful, because it narrows the causes to three. The struct does not exist anywhere in the program, the struct exists in a module this scope has not imported, or the name in the literal is spelled differently from the struct that exists. The three fixes below are those three causes, in that order.
The error: a struct literal with no struct behind it
This program builds a Point value and prints its fields, but nothing in it defines a Point:
fn main() {
let point = Point { x: 1, y: 2 };
println!("{} {}", point.x, point.y);
}
$ rustc --edition 2021 broken.rs -o broken.bin
error[E0422]: cannot find struct, variant or union type `Point` in this scope
--> broken.rs:2:17
|
2 | let point = Point { 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 caret sits on the name alone, not on the braces or the fields: the compiler never gets as far as checking whether x and y are the right fields, because there is no struct definition to check them against. The label is "not found in this scope", and there is no help section — no import to suggest and no similar name to offer, because no linked item the compiler can see matches. A silent help section is itself a signal: when the compiler can name the fix, the item exists somewhere it can see; when it cannot, the missing piece is most likely the definition itself.
The fix: define the struct
When the type is the program's own and it simply was never written, the repair is the definition. A struct definition names the type and each field with its type; after it, the same literal the error rejected builds the value:
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Point { x: 1, y: 2 };
println!("{} {}", point.x, point.y);
}
$ rustc --edition 2021 fixed_define.rs -o fixed_define.bin
$ ./fixed_define.bin
1 2
Defining the struct is also what gives the field list something to be checked against from then on: a literal that names a field the struct does not have, or leaves one out, now fails against this definition instead of failing on the name. If the program is a sketch and the shape of the data is still being decided, this is the moment to decide it — the fields and their types in the definition are the contract every literal in the program will be held to.
The fix: import the struct from its module
When the struct already exists — defined once, in a module — the literal fails only because the name is not in scope where the literal is written. A use declaration brings the name into the current scope; the definition itself does not move or change:
mod geometry {
pub struct Point {
pub x: i32,
pub y: i32,
}
}
use geometry::Point;
fn main() {
let point = Point { x: 1, y: 2 };
println!("{} {}", point.x, point.y);
}
$ rustc --edition 2021 fixed_import.rs -o fixed_import.bin
$ ./fixed_import.bin
1 2
Two details in this version are load-bearing. The struct and its fields are marked pub: a struct defined in another module is only usable outside that module where it is visible, and each field the literal sets must be visible too. And the import names the struct itself, geometry::Point, so the literal can use the short name; without the use line, the same program would need the full path in the literal, geometry::Point { x: 1, y: 2 }. Prefer the import when the type is used more than once in the file; prefer the full path for a single use, where an import line is more machinery than the use earns.
The fix: correct a misspelled name
The third cause is a name that is almost the struct's name. Here the struct exists in the same file, spelled Point, and the literal writes Piont:
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Piont { x: 1, y: 2 };
println!("{} {}", point.x, point.y);
}
$ rustc --edition 2021 broken_typo.rs -o broken_typo.bin
error[E0422]: cannot find struct, variant or union type `Piont` in this scope
--> broken_typo.rs:7:17
|
1 | struct Point {
| ------------ similarly named struct `Point` defined here
...
7 | let point = Piont { x: 1, y: 2 };
| ^^^^^
|
help: a struct with a similar name exists
|
7 - let point = Piont { x: 1, y: 2 };
7 + let point = Point { x: 1, y: 2 };
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0422`.
This time the report does carry help, and it is exact: the compiler points back at the definition line, names it as a similarly named struct, and prints the corrected line as a diff. Taking the suggestion literally — the literal now spells the name the way the definition does — is the whole fix:
struct Point {
x: i32,
y: i32,
}
fn main() {
let point = Point { x: 1, y: 2 };
println!("{} {}", point.x, point.y);
}
$ rustc --edition 2021 fixed_typo.rs -o fixed_typo.bin
$ ./fixed_typo.bin
1 2
The same shape covers a rename that only half landed: the struct was renamed in its definition and one literal kept the old name, or the literal was updated and the definition was not. Either way the similar-name note identifies the pair, and the repair is to make the two spellings agree — in whichever direction the rename intended. When no similar name exists, the compiler offers no suggestion, and the cause is one of the first two instead: define the struct, or import it.
The same missing name in other positions
The name Point with no definition behind it produces three different codes in three positions, and telling them apart keeps a fix aimed at the right guide. In a parameter type, the report is E0425:
fn describe(point: Point) {
println!("{} {}", point.x, point.y);
}
fn main() {}
$ rustc --edition 2021 probe_annotation.rs -o probe_annotation.bin
error[E0425]: cannot find type `Point` in this scope
--> probe_annotation.rs:1:20
|
1 | fn describe(point: Point) {
| ^^^^^ not found in this scope
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
At the head of a path, the report is E0433:
fn main() {
let point = Point::origin();
println!("{:?}", point);
}
$ rustc --edition 2021 probe_path.rs -o probe_path.bin
error[E0433]: cannot find type `Point` in this scope
--> probe_path.rs:2:17
|
2 | let point = Point::origin();
| ^^^^^ use of undeclared type `Point`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0433`.
And a struct literal whose name belongs to a local variable is E0574, not E0422 — the name resolved, just in the wrong namespace:
fn main() {
let foo = 1;
let x = foo { x: 1, y: 2 };
println!("{:?}", x);
}
$ rustc --edition 2021 trap_variable.rs -o trap_variable.bin
error[E0574]: cannot find struct, variant or union type `foo` in this scope
--> trap_variable.rs:3:13
|
3 | let x = foo { x: 1, y: 2 };
| ^^^ not found in this scope
|
= note: a local variable named `foo` exists in another namespace
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0574`.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Struct never defined anywhere in the program | E0422, no help section offered | Define the struct with its fields and their types |
| Struct defined in a module this scope has not imported | E0422, name not found in this scope | Add a use for the struct, or write the literal with its full path |
| Literal's name is a near-miss spelling of a defined struct | E0422, with a "similarly named struct" note and a printed correction | Spell the name exactly as the definition does |
| Same name in a type annotation or parameter type | E0425, cannot find type | Define or import the type — the E0425 guide covers this position |
Same name at the head of a path, as in Point::origin() |
E0433, use of undeclared type | Define or import the type — the E0433 guide covers this position |
| Literal's name is a local variable | E0574, with a note that a local variable of that name exists | Rename the variable or the struct so the two stop colliding |
Checklist
- Read the position before the name. E0422 only fires on a name directly followed by curly brackets; the same missing name in an annotation is E0425 and at the head of a path is E0433, and each has its own repair path.
- Look for the help section. A "similarly named struct" note with a printed correction means the struct exists and the literal's spelling is the defect — take the printed correction. No help at all usually means the definition itself is missing.
- Search the program for the definition before writing one. If a
structwith that name exists in another module, the fix is auseimport, not a second definition — two definitions of the same name in one scope trade E0422 for a duplicate-definition error. - Check visibility when importing. A struct used from another module must be
pub, and every field the literal sets must bepubas well; a private field fails with its own error once the name resolves. - Make renames land everywhere at once. A struct renamed in its definition but not in its literals — or the reverse — comes back as E0422 with the other spelling offered as the similar name.
- Do not define a placeholder struct just to silence the name. The fields are the contract the rest of the program is checked against; a guessed definition moves the failure to every field access that follows.
Related guides on this site
- E0425 Cannot Find Value Fix — the neighbouring error for a name that cannot be found in this scope in value and type positions, including the same missing type name used in an annotation, where this guide covers the name in struct-literal position only.
- E0433 Failed to Resolve Fix — the error for a name at the head of a path that does not resolve, such as a type's associated function called on a type that was never imported or defined.
- E0063 Missing Fields in Struct Fix — the error that comes after this one: the struct name resolves and the literal builds against a real definition, but the literal leaves out one or more of the struct's fields.
Sources
- Rust Compiler Error Index, E0422 — for an identifier that is neither defined nor a struct being used, for the example of a struct literal naming an undefined
Foo, and for the case of a name that is defined but is not a struct. - The Rust Programming Language, Defining and Instantiating Structs — for defining a struct with named fields and their types, and for creating an instance by stating the name of the struct followed by curly brackets containing key: value pairs for the fields.
- The Rust Reference, Struct expressions — for a struct expression consisting of a path followed by curly brackets containing the field values, the form whose leading name E0422 reports on.
- The Rust Reference, Use declarations — for
usedeclarations bringing a name into the current scope, the mechanism behind the import fix.
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.