Rust error message to fix a typo

rustc error code

Here is one thing for which I like Rust.

Recently, as I wrote some code, I made a typo in a variable name:

examples/code_with_typo.rs

fn main() {
    zero(3);
    zero(0);

}

fn zero(n: i32) {
    if i == 0 {
        println!("zero");
    } else {
        println!("{} is not zero", n);
    }
}

Running the rust compiler on this file rustc examples/code_with_typo.rs I got the following error:

rustc examples/code_with_typo.rs
error[E0425]: cannot find value `i` in this scope
 --> examples/code_with_typo.rs:8:8
  |
8 |     if i == 0 {
  |        ^ help: a local variable with a similar name exists: `n`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.

So somehow Rust understood that the variable i might somehow be releated to the existing name n.

Nice.

Related Pages

Compiler Error codes in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo