What the code is supposed to do
The program should define a function welcome_user and call it. The function prints two messages, and they must appear in this order:
Hey coder!
Welcome back!
The starting code prints them the other way around:
welcome_user();
fn welcome_user() {
println!("Welcome back!");
println!("Hey coder!");
}
It prints Welcome back! first and Hey coder! second. The task is to fix the code so the output matches.
What goes wrong
There are two things to look at: the position of the call and the order of the statements inside the function.
The order inside the body
The statements in a function body run from top to bottom, exactly in the order they are written. The body has println!("Welcome back!"); on its first line and println!("Hey coder!"); on its second, so the output has the same order. This is the cause of the wrong output. The fix is to swap the two lines.
The call before the definition
The call welcome_user(); sits above the definition. That is not what produces the wrong output, because Rust lets code use a function that is defined later in the same scope. The compiler reads the whole scope before it checks the calls. Moving the call below the definition is still good practice, because a reader meets the function before they see it used. The lesson's fix includes this move.
Fixing it step by step
- Swap the two
println!lines inside the function, soHey coder!comes first andWelcome back!second. - Move the call below the definition. Define the function first, then call it. The output does not change, but the code now reads from top to bottom in the order it is explained.
- Keep the call. Unlike the lesson on defining functions, this program should print, so exactly one call is needed. Without it, nothing is printed.
The fixed code in Rust
fn welcome_user() {
println!("Hey coder!");
println!("Welcome back!");
}
welcome_user();
/* Explanation:
There are two problems with the original code:
1. Although calling a function before defining it works in Rust,
it's a good practice to call a function only after defining it.
For Code Quality reasons, we'll move the function call after (below) the function definition in our code.
2. The statements inside a function are executed in the exact order we write them.
That's why "Welcome back!" gets printed before "Hey coder!" in the console.
We have to swap the println!() statements' order inside our function.
*/
Details worth knowing:
- Top-to-bottom execution. Inside a body, each statement finishes before the next begins. There is no reordering.
- Definition order versus execution order. Where a function is defined in the file does not decide when it runs. The call decides that. Where the statements sit inside the body decides the order of the effects.
- Functions defined inside another function. A
fnwritten inside the body of another function, such asmain, can be called from anywhere in that same body, even above its definition. The same is true for functions at the top level of a file. - Calls end with a semicolon.
welcome_user();is a statement.
Common mistakes
- Fixing only the call position. Moving the call below the definition does not change the output. The two
println!lines still print in the wrong order. - Fixing only the order of the lines. The output is then correct, but the code still calls the function above its definition. The reference fix also moves the call below the definition, which is the more readable order.
- Calling twice. Keeping the original call and adding a second one prints both messages twice.
- Editing the text. The messages must match exactly:
Hey coder!andWelcome back!, with the capital letters and exclamation marks.
Checking your fix
Run the code in the editor above. A correct fix prints two lines: Hey coder! followed by Welcome back!.
