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 error
Section titled “The error”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:
fn main() { let username = "alice"; println!("logged in as {usernane}");}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:
fn main() { let username = "alice"; println!("logged in as {username}");}This prints:
logged in as aliceWhen the variable is in a different scope
Section titled “When the variable is in a different scope”The second shape has no typo — the variable genuinely exists, but it was declared inside a block that has already ended:
fn main() { let total = 42; if total > 10 { let discount = 5; println!("applying {discount}"); } println!("discount is {discount}");}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:
fn main() { let total = 42; if total > 10 { let discount = 5; println!("applying {discount}"); }}This prints:
applying 5If 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:
fn main() { let total = 42; let discount = if total > 10 { 5 } else { 0 }; println!("discount is {discount}");}This prints:
discount is 5When 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:
fn main() { let tau = 2.0 * PI; println!("tau is {tau}");}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:
use std::f64::consts::PI;
fn main() { let tau = 2.0 * PI; println!("tau is {tau}");}This prints:
tau is 6.283185307179586The same shape with a value from your own code — a constant in a module of the same project:
mod config { pub const PORT: u16 = 8080;}
fn main() { println!("listening on {PORT}");}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:
mod config { pub const PORT: u16 = 8080;}
use crate::config::PORT;
fn main() { println!("listening on {PORT}");}This prints:
listening on 8080Writing 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.
Not this error
Section titled “Not this error”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.