Rust「cannot find value in this scope」(E0425)の原因と直し方
cannot find value in this scope(E0425)は、変数を見える場所で宣言するか use で取り込めば直る。
綴りの誤り、ブロックの外での参照、self. の付け忘れでも出る。
公開: 更新:
rustc bad_block.rs … error[E0425]: cannot find value `total` in this scope --> bad_block.rs:6:20 | 6 | println!("{}", total);
rustc solve_use.rs
2026-11-24 に rust:1(Rust 1.100.0) で実際に打って取った出力。検証環境の詳細
要約
cargo build や rustc が次のエラーで止まることがある。
error[E0425]: cannot find value `total` in this scopecannot find value in this scope は、その場所から見える範囲(スコープ)に、その名前の変数や定数が無いという意味である(E0425(新しいタブで開く))。
名前が見える場所で宣言するか、use で取り込めば直る。
関数で同じことが起きると cannot find function になり、エラー番号は同じ E0425 である。
rustc はエラーの下に help: で直し方の候補を出す。
まずそこを読むと、どの原因かがすぐ分かる。
実行例
rust:1 で rustc を動かすと、ブロックの外で内側の let を使った場合・self. を付けずにフィールドを読んだ場合・use なしで別モジュールの定数を使った場合の 3 つとも E0425 で終了コード 1 になる。if を式にする・self.count と書く・use config::MAX; を足すと、どれもコンパイルが通って実行できる。
$ rustc bad_block.rs
error[E0425]: cannot find value `total` in this scope
--> bad_block.rs:6:20
|
6 | println!("{}", total);
| ^^^^^
|
help: the binding `total` is available in a different scope in the same function
--> bad_block.rs:4:13
|
4 | let total = 10;
| ^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
$ echo $?
1$ rustc solve_block.rs
$ echo $?
0$ ./solve_block
10$ rustc bad_self.rs
error[E0425]: cannot find value `count` in this scope
--> bad_self.rs:7:9
|
7 | count += 1;
| ^^^^^
|
help: you might have meant to use the available field
|
7 | self.count += 1;
| +++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
$ echo $?
1$ rustc solve_self.rs
$ echo $?
0$ ./solve_self
1$ rustc bad_use.rs
error[E0425]: cannot find value `MAX` in this scope
--> bad_use.rs:6:20
|
6 | println!("{}", MAX);
| ^^^ not found in this scope
|
help: consider importing one of these constants
|
1 + use std::char::MAX;
|
1 + use std::f32::MAX;
|
1 + use std::f64::MAX;
|
1 + use std::i128::MAX;
|
= and 12 other candidates
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
$ echo $?
1$ rustc solve_use.rs
$ echo $?
0$ ./solve_use
3— 2026-11-24 時点の出力
検証環境Python 3.13.5 / Git 2.47.3 / Rust 1.100.0 / 2026-11-24 検証
- 検証日
- 実行環境
rust:1Debian GNU/Linux 13 (trixie)- バージョン
- Python 3.13.5
- Git 2.47.3
- Rust 1.100.0
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. ブロックの外で使う値は外で受け取る
let total = if ready { 10 } else { 0 };
println!("{}", total);if は値を返せるので、ブロックの中で let せずに、外の let で結果を受け取る。
2. フィールドには self. を付ける
impl Counter {
fn bump(&mut self) {
self.count += 1;
}
}3. 別のモジュールの定数と関数は use で取り込む
mod config {
pub const MAX: u32 = 3;
}
use config::MAX;
fn main() {
println!("{}", MAX);
}use の書き方は The Rust Programming Language(新しいタブで開く) の 7 章にある。
定義したモジュール(ここでは config)の外から使うなら、定義に pub が要る。pub が無いと E0603(private)になる。
use を書かずに config::MAX とパスごと書いても通る。config というモジュールやクレート自体が見つからないときは E0425 にならない。use config::MAX; なら E0432、パスで書いた config::MAX なら E0433 になる。
よくある原因
- 名前を打ち間違えている。
rustc 1.99 では、help: a local variable with a similar name existsと正しい名前の候補が出た。 - ブロックの中で宣言した変数を、ブロックの外で使っている。
ifの中のlet totalは、}を出たところで見えなくなる。
rustc 1.99 はis available in a different scope in the same functionというhelp:で、宣言した行を示した。 - メソッドの中で、構造体のフィールドを名前だけで読んでいる。
Rust はフィールドをself.無しでは探さないので、help: you might have meant to use the available fieldが出る。 - 別のモジュールの定数を
useせずに使っている。
rustc 1.99 のhelpは先頭に自分のuse crate::config::MAX;を出し、続けて標準ライブラリのstd::f64::MAXなども並べた。
後者は自分のモジュールの定数とは別物である。