Skip to content
Select theme

How to fix error E0425: cannot find value in this scope

Short answer: the compiler met a name it has never seen anywhere it can reach. The value is either misspelled, declared in a narrower scope than the line using it, or never imported. The error itself just points at the name — the help: block underneath it tells you which of the three happened, and usually offers the fix.

The most common shape is the typo — and the most common place for one is inside a format string, where the name in {} is a real variable lookup, not just text:

Compiling that with cargo build prints:

error[E0425]: cannot find value `usernane` in this scope
--> src/main.rs:3:29
|
3 | println!("logged in as {usernane}");
| ^^^^^^^^
|
help: a local variable with a similar name exists
|
3 - println!("logged in as {usernane}");
3 + println!("logged in as {username}");
|
For more information about this error, try `rustc --explain E0425`.

cannot find value in this scope means name resolution failed: the compiler read the whole function, collected every binding it could see, and usernane matched none of them. Unlike a type mismatch, there is no intent to recover from — a name it has never heard of has no reasonable interpretation. So the error stops there, but the compiler does not: the help: block compares the unknown name against every local binding and offers the closest one, which here is the fix itself. Accept the suggestion:

This prints:

logged in as alice

The second shape has no typo — the variable genuinely exists, but it was declared inside a block that has already ended:

error[E0425]: cannot find value `discount` in this scope
--> src/main.rs:7:28
|
7 | println!("discount is {discount}");
| ^^^^^^^^
|
help: the binding `discount` is available in a different scope in the same function
--> src/main.rs:4:13
|
4 | let discount = 5;
| ^^^^^^^^
For more information about this error, try `rustc --explain E0425`.

A let binding lives from its own line to the end of the block it was declared in — here, the braces of the if. The last println! is outside those braces, so from where it stands the name does not exist. This is the one case where the compiler knows exactly where the value is, and the help: block says so: the binding discount is available in a different scope in the same function, pointing at line 4.

The fix depends on which line is wrong. If the use belongs inside the block, move it in:

This prints:

applying 5

If the value is genuinely needed after the block, it has to be declared outside it — and the cleanest way to keep the condition attached to the value is an if expression:

This prints:

discount is 5

When the value exists, but in another module

Section titled “When the value exists, but in another module”

The third shape looks like the scope problem but is really an import problem. Nothing from std — or from your own modules — is in scope until a use brings it in:

error[E0425]: cannot find value `PI` in this scope
--> src/main.rs:2:21
|
2 | let tau = 2.0 * PI;
| ^^ not found in this scope
|
help: consider importing one of these constants
|
1 + use std::f32::consts::PI;
|
1 + use std::f64::consts::PI;
|
For more information about this error, try `rustc --explain E0425`.

PI exists in the standard library, but it lives at std::f64::consts::PI, and paths from other modules are not in scope by default. The help: block found the constant and offers the import — pick the f64 one, because 2.0 is an f64:

This prints:

tau is 6.283185307179586

The same shape with a value from your own code — a constant in a module of the same project:

error[E0425]: cannot find value `PORT` in this scope
--> src/main.rs:6:29
|
6 | println!("listening on {PORT}");
| ^^^^ not found in this scope
|
help: consider importing this constant
|
1 + use crate::config::PORT;
|
For more information about this error, try `rustc --explain E0425`.

Defining something pub in a module makes it visible to the rest of the crate — but only by path. The name PORT on its own is still unknown, and the fix is the import the help suggests:

This prints:

listening on 8080

Writing the path at the use site — println!("listening on {}", config::PORT); — works too, and for a value used once it is the lighter fix. The use form wins when the name appears many times, or when the path would be deep.

E0425 is about value names — variables, constants, functions. When the failing name is a type or a path instead (HashMap::new() without the import, for example), the error is E0433, failed to resolve; and when it is the use line itself that will not compile, that is E0432, unresolved import — the import side of the same story. The compiler’s pick between them follows the kind of name, not the wording of the message.