Skip to content

Rust Error E0425: Cannot Find Value in This Scope

Error E0425 means Rust looked for a name at one exact spot in your code and couldn’t find it there. It’s one of the most common errors there is, and it’s almost always quick to fix: usually a typo, a variable that lives inside a different block, or a function that needs its module path. This page is part of the compiler errors guide.

error[E0425]: cannot find value `total_scor` in this scope
--> src/main.rs:3:33
|
3 | println!("Final score: {}", total_scor);
| ^^^^^^^^^^
|
help: a local variable with a similar name exists
|
3 | println!("Final score: {}", total_score);
| +

This is the program that produced it:

fn main() {
let total_score = 42;
println!("Final score: {}", total_scor);
}

The ^^^^ marks the name Rust couldn’t find: total_scor on line 3. “In this scope” means “from where this line stands”. Rust checked every variable, function and import that is visible at that point, and nothing had that name.

Read the help: line too. Here rustc spotted a variable with a similar name (total_score) and even shows the missing letter with a +. When you see “a local variable with a similar name exists”, it’s a typo.

Think of it like calling “Jon!” across a room when your friend is named John. He’s right there, but nobody answers to “Jon”.

The same error also shows up as cannot find function ... in this scope when the missing name is a function you call.

Rust decides, before your program runs, what every name refers to. A variable only exists from its let line until the closing } of the block it was declared in. That’s its scope. Outside that region the name simply doesn’t exist.

Many languages would let you get away with this, and you’d find out later. In JavaScript, a typo can quietly create a new global variable or give you undefined. In Python you’d get a NameError at runtime, maybe only on the one code path your tests never hit. Rust catches it at compile time, every time.

Functions follow the same idea with modules: a function inside mod billing isn’t visible from main until you name its path or bring it in with use.

Choose this when the help: line says a similar name exists. It’s the most common cause by far. Rust names are case-sensitive too, so Total_score and total_score are different names.

fn main() {
let total_score = 42;
println!("Final score: {}", total_score);
}

Output:

Final score: 42
▶ Try it Yourself

Fix 2: Declare the variable where both places can see it

Section titled “Fix 2: Declare the variable where both places can see it”

Choose this when the help says “the binding is available in a different scope in the same function”. You made the variable inside a loop or if block, then used it after the }. That broken version looks like this:

fn main() {
let prices = [120, 300, 80];
for price in prices {
let with_tax = price + price / 10;
println!("{with_tax}");
}
println!("Last price with tax: {}", with_tax); // error[E0425]
}

with_tax is created fresh on each pass through the loop and thrown away at the loop’s }. If you need a value after the loop, declare it before the loop and update it inside:

fn main() {
let prices = [120, 300, 80];
let mut last_with_tax = 0;
for price in prices {
let with_tax = price + price / 10;
println!("{with_tax}");
last_with_tax = with_tax;
}
println!("Last price with tax: {}", last_with_tax);
}

Output:

132
330
88
Last price with tax: 88
▶ Try it Yourself

The same fix works if you used a variable before its let line. Rust reads top to bottom, so just move the let up.

Fix 3: Give the function’s path, or add a use

Section titled “Fix 3: Give the function’s path, or add a use”

Choose this for cannot find function when the function lives in another module. Rust prints:

error[E0425]: cannot find function `total_with_tax` in this scope
--> src/main.rs:8:17
|
8 | let total = total_with_tax(50.0);
| ^^^^^^^^^^^^^^ not found in this scope
|
help: consider importing this function
|
1 + use crate::billing::total_with_tax;
|

Either call it with its full path (billing::total_with_tax(50.0)) or add the use line rustc suggests:

mod billing {
pub fn total_with_tax(amount: f64) -> f64 {
amount * 1.2
}
}
use crate::billing::total_with_tax;
fn main() {
let total = total_with_tax(50.0);
println!("{total}");
}

Output:

60
▶ Try it Yourself

The function also has to be pub. Without it you’d get a different error (E0603, “function is private”). See Modules for how paths and use work.

Choose this inside a method, when the missing name is one of your struct’s fields. Coming from Java or C#, it’s natural to write count and expect it to mean the field. In Rust you always write self.count. rustc says: help: you might have meant to use the available field.

struct Counter {
count: u32,
}
impl Counter {
fn report(&self) {
println!("Count: {}", self.count);
}
}
fn main() {
let c = Counter { count: 3 };
c.report();
}

Output:

Count: 3
▶ Try it Yourself
  • A variable set inside an if. if balance > 50 { let bonus = 10; } and then println!("{}", bonus) fails, because bonus died at the }. Fix: make the if produce the value, let bonus = if balance > 50 { 10 } else { 0 };.

  • Calling a constructor without its type. let p = new("ferris"); gives cannot find function `new` in this scope. Fix: new belongs to the type, so write Player::new("ferris").

  • Using self in a function that has no self parameter. This is E0425’s close cousin, E0424:

    error[E0424]: expected value, found module `self`
    --> src/main.rs:7:9
    |
    6 | fn reset() {
    | ----- this function doesn't have a `self` parameter
    7 | self.count = 0;
    | ^^^^ `self` value is a keyword only available in methods with a `self` parameter

    Fix: add a self parameter. rustc suggests &self, but this one changes a field, so it needs fn reset(&mut self).

Quick check

1. rustc says "help: a local variable with a similar name exists". What's the most likely cause?

2. You write let found = true; inside a for loop and use found after the loop. Why does it fail?

3. A function send lives in mod mailer. Which line lets main call it as send()?

Score: 0 / 3