Start learning free

LESSON 38 OF 414 · FUNCTIONS I

Fixing "cannot find value" for a Parameter in Rust

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that takes a number as argument and prints the double of that number to the console.

So when we ran the code, we expected it to print:

20

but it seems like we made some mistakes because when we run our code we get an error instead.


Assignment:

Your task is to fix our code such that no errors will be produced and it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    RUST · FUNCTIONS I

    Buggy Code Non Existing Variable in Rust, explained

    What the code is supposed to do

    The function printDouble takes a number n and should print twice that number. The call printDouble(10) should print 20. Instead the program does not compile.

    fn printDouble(n: i32) {
        println!("{}", 2 * num);
    }
    fn mainFunction() {
        printDouble(10);
    }
    

    What goes wrong

    The compiler reports an error of this kind:

    error[E0425]: cannot find value `num` in this scope
     --> 2:24
      |
    2 |     println!("{}", 2 * num);
      |                        ^^^ not found in this scope
    

    The function declares a parameter called n, but the body multiplies by num. No variable named num exists anywhere the function can see. A function body can use only its own parameters, variables it creates with let inside it, and items defined at the top level of the file such as other functions. Rust does not guess that num was meant to be n.

    Because the error stops compilation, nothing runs and nothing prints.

    Fixing it step by step

    1. Read the error. It names the identifier it could not find, num, and points at the line that uses it.
    2. Compare with the parameter list. The parameter is n. The body uses a different name.
    3. Make the two agree. Change num to n in the println! line.

    The fixed code in Rust

    fn printDouble(n: i32) {
        println!("{}", 2 * n);
    }
    fn mainFunction() {
        printDouble(10);
    }
    /* Explanation:
    We got an error because inside the original function we used a variable "num", which is not the parameter "n", so it wasn't recognized.
    One fix is to use "n" instead of "num" in the line "println!(2 * n)"
    Another possible fix is to change the parameter name to "num", like this:
    fn printDouble(num: i32) {
        println!("{}", 2 * num);
    }
    */
    

    With n = 10, the expression 2 * n is 20, and println! prints it. The {} in the format string is where the value goes.

    The comment block shows the other valid fix: rename the parameter to num instead. Both versions behave identically. What matters is that the name in the parameter list and the name in the body match exactly, including capitalization, because N and n are different names in Rust.

    Why parameter names matter

    A parameter is a local variable that exists only while the function runs. Its name is how the body refers to the value that was passed in. When you call printDouble(10), Rust binds the value 10 to the name n for the duration of that call. Any other name has no value attached to it.

    This is why descriptive names are worth choosing carefully. A name such as n is short, so typing a longer name in the body by mistake is easy. The compiler catches the slip at build time, before the program ever runs, so a mismatched name never turns into a wrong answer at run time. Rust checks every name in the whole function before it produces any output.

    Common mistakes

    • Fixing only the call. The call printDouble(10) is fine. The mistake is inside the function.
    • Renaming in only one place. If you rename the parameter to num, the body must use num as well. Changing one side recreates the same error in reverse, this time reporting that n cannot be found if the body still uses it.
    • Using a variable from another function. A let inside mainFunction is invisible inside printDouble. Pass the value in as an argument instead.

    Reading the error message

    Rust's messages are worth reading closely. The code E0425 means an unresolved name. The caret marks (^^^) underline exactly the word the compiler could not match, and the line number tells you where to look. When you see "cannot find value", check three things: a typo in the name, a variable that was created in a different function, and a variable used before its let.

    Checking your fix

    Run the code in the editor above. A correct fix compiles without errors and prints 20. The names printDouble and mainFunction are not snake_case, so the compiler may warn about them, but warnings do not stop the program.

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free