Rust エラー「error[E0425]: cannot find value xxx in this scope」が発生した場合の対処法

  • 作成日 2022.10.08
  • 更新日 2022.12.02
  • Rust
Rust エラー「error[E0425]: cannot find value xxx in this scope」が発生した場合の対処法

Rustで、エラー「error[E0425]: cannot find value xxx in this scope」が発生した場合の対処法を記述してます。Rustのバージョンは1.62.0を使用してます。

環境

  • OS windows11 home
  • rustc 1.62.0

エラー全文

以下のコードで発生

fn main() {

    let str: String = "mebee".to_string();

    println!("{}", foo);

}

エラー全文

error[E0425]: cannot find value `foo` in this scope
 --> sample.rs:5:20
  |
5 |     println!("{}", foo);
  |                    ^^^ not found in this scope

error: aborting due to previous error

原因

「foo」が定義されていないため

対処法

「foo」を定義する

fn main() {

    let foo: String = "mebee".to_string();

    println!("{}", foo);

}

実行結果