E0425: cannot find value in this scope の原因と直し方
宣言していない変数名を参照すると、コンパイル時にE0425エラーになります。変数名のタイプミスが典型的な原因です。
エラーメッセージの読み方
error[E0425]: cannot find value `pricee` in this scope
E0425- エラーコード — `rustc --explain E0425` で詳しい説明が見られます
cannot find value `pricee` in this scope- 詳細メッセージ — 何が問題だったか
このエラーが出る典型パターン
パターン1
1 fn main() { 2 let price = 1000; 3 println!("{}", pricee); ^ 4 }
error[E0425]: cannot find value `pricee` in this scope
priceとpriceeは1文字違うだけでも別の名前として扱われ、コンパイラはpriceeを見つけられません。
直し方: pricee を price にします。
広告
広告スロット(未設定)
パターン2
1 fn main() { 2 let score = 88; 3 println!("{}", scroe); ^ 4 }
error[E0425]: cannot find value `scroe` in this scope
文字の順番が入れ替わったタイプミスも、Rustコンパイラには全く別の名前に見えます。
直し方: scroe を score にします。
パターン3
1 fn main() { 2 let total = 250; 3 println!("{}", totall); ^ 4 }
error[E0425]: cannot find value `totall` in this scope
1文字多いだけでも一致しない名前として扱われ、宣言されたことのない値の参照になります。
直し方: totall を total にします。
パターン4
1 fn main() { 2 let username = "kenji"; 3 println!("{}", usrname ); ^ 4 }
error[E0425]: cannot find value `usrname` in this scope
コンパイラは似た名前を提案してくれますが、実行はしてくれません。正しい名前を書く必要があります。
直し方: usrname を username にします。
パターン5
1 fn main() { 2 let temperature = 25; 3 println!("{}", temprature ); ^ 4 }
error[E0425]: cannot find value `temprature` in this scope
長い変数名ほどタイプミスに気づきにくくなります。宣言した名前と完全に一致しているか確認してください。
直し方: temprature を temperature にします。
よくある誤解
「似た名前の変数があれば、コンパイラが自動的にそれだと解釈してくれるはず」という思い込みは誤りです。コンパイラは似た名前を提案(help)はしますが、勝手に補完して実行することはありません。
まとめ
E0425: cannot find value in this scopeは初級でつまずきやすい項目です。上の5パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。