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
- Read the error. It names the identifier it could not find,
num, and points at the line that uses it. - Compare with the parameter list. The parameter is
n. The body uses a different name. - Make the two agree. Change
numtonin theprintln!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 usenumas well. Changing one side recreates the same error in reverse, this time reporting thatncannot be found if the body still uses it. - Using a variable from another function. A
letinsidemainFunctionis invisible insideprintDouble. 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.
