Problem overview
The quiz shows two functions and asks what the code prints:
fn greet_coder() {
println!("Hello!");
println!("Let's code!");
}
fn main_function() {
greet_coder();
greet_coder();
}
The options are four possible outputs:
- A:
Hello!,Let's code!,Hello!,Let's code! - B:
Hello!,Hello!,Let's code!,Let's code! - C:
Hello!,Let's code!once - D: the pair of lines three times
The quiz treats main_function as the program's main part, the code that runs. The answer is recorded in a comment and printed with println!.
Approach: trace the calls in order
Treat each function call as "run the whole body here, then continue." Follow the code from the top of main_function and write down what appears.
- The definitions run nothing.
greet_coderandmain_functionare stored for later. - First call.
greet_coder();runs both statements of its body in order:Hello!, thenLet's code!. The call finishes and control returns tomain_function. - Second call. The next line calls
greet_coder();again. The whole body runs again from its first line:Hello!, thenLet's code!.
The output is the four lines of option A, in that order.
Why not B
Option B would need the first call to stop after its first line and the second call to start before the first has finished. A function call always runs the entire body before the next statement after the call starts, so the two lines of a call stay together.
Why not C or D
Option C is one call's output. There are two calls, so the output is two copies. Option D is three copies, but the code has only two call lines.
A way to check yourself
Write the output down line by line while you trace. Put a mark next to each call and list the lines it adds: the first call adds two lines and the second adds two more. A list of four lines in the order Hello!, Let's code!, Hello!, Let's code! is option A. Counting calls and multiplying by the lines in the body is a quick sanity check on any quiz of this kind.
The solution in Rust
// answer = "A"
println!("A");
/* Explanation:
In the first 4 lines of code we only define the function, so no "println!()" statement will actually get executed.
Then, inside the main function, we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!
Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the output is:
Hello!
Let's code!
Hello!
Let's code!
*/
Some Rust details:
- Each
println!adds a line break. That is why every message appears on its own line. - Calling is name plus parentheses plus semicolon.
greet_coder();runs the function. Without parentheses,greet_coderis just the name and runs nothing. - Rust itself starts at
main. In a complete Rust program, execution begins in a function named exactlymain. The quiz uses the namemain_functionfor its main part, so for this lesson read it as the code that runs. A compiled file withgreet_coderandmain_functionbut nomainwould not build as a program.
Edge cases and common mistakes
- Counting the definitions as output. Defining a function prints nothing. Only calls run the body.
- Interleaving the lines. Seeing
Hello!twice in a row is the most common wrong answer (option B). A call completes before the next one begins. - Counting the wrong number of calls. Count the call lines, not the function names. Here, two lines call
greet_coder. - Letter mismatch. The comment and the printed letter must both be
A.
Testing the idea
- With one call, the output is
Hello!andLet's code!, which is option C. - With three calls, the output is the pair of lines three times, which is option D.
- With two calls, the output is the pair of lines twice, which is option A.
