Bird
Raised Fist0
Rustprogramming~20 mins

Function call flow in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Function Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code?

Consider the following Rust program. What will it print when run?

Rust
fn greet() {
    println!("Hello from greet!");
}

fn main() {
    println!("Start main");
    greet();
    println!("End main");
}
A
Start main
Hello from greet!
End main
B
Hello from greet!
Start main
End main
C
Start main
Hello from greet!
D
Start main
End main
Hello from greet!
Attempts:
2 left
💡 Hint

Think about the order in which functions are called and how println! outputs text.

❓ Predict Output
intermediate
2:00remaining
What is the output of nested function calls?

What will this Rust program print?

Rust
fn first() {
    println!("First");
}

fn second() {
    println!("Second start");
    first();
    println!("Second end");
}

fn main() {
    second();
}
A
Second start
Second end
First
B
Second start
First
Second end
C
First
Second start
Second end
D
Second end
First
Second start
Attempts:
2 left
💡 Hint

Remember that when a function calls another, the called function runs completely before returning.

❓ Predict Output
advanced
2:30remaining
What is the output with recursive function calls?

What will this Rust program print?

Rust
fn countdown(n: u32) {
    if n == 0 {
        println!("Blast off!");
    } else {
        println!("{}", n);
        countdown(n - 1);
    }
}

fn main() {
    countdown(3);
}
A
3
2
1
Blast off!
B
Blast off!
1
2
3
C
3
2
1
0
Blast off!
D
3
2
Blast off!
1
Attempts:
2 left
💡 Hint

Think about how recursion works and when the base case triggers.

❓ Predict Output
advanced
2:30remaining
What is the output of this program with multiple function calls?

What will this Rust program print?

Rust
fn a() {
    println!("A");
}

fn b() {
    println!("B start");
    a();
    println!("B end");
}

fn c() {
    println!("C start");
    b();
    println!("C end");
}

fn main() {
    c();
}
A
C start
A
B start
B end
C end
B
A
B start
B end
C start
C end
C
C start
B start
B end
A
C end
D
C start
B start
A
B end
C end
Attempts:
2 left
💡 Hint

Follow the chain of calls from main to c, then b, then a.

🧠 Conceptual
expert
3:00remaining
What happens to the call stack during these function calls?

Given the following Rust code, what is the state of the call stack just before println!("Inside inner"); executes?

Rust
fn inner() {
    println!("Inside inner");
}

fn middle() {
    inner();
}

fn outer() {
    middle();
}

fn main() {
    outer();
}
AThe call stack has <code>main</code> at bottom, then <code>middle</code>, then <code>outer</code>, then <code>inner</code> at top.
BThe call stack has <code>inner</code> at bottom, then <code>middle</code>, then <code>outer</code>, then <code>main</code> at top.
CThe call stack has <code>main</code> at bottom, then <code>outer</code>, then <code>middle</code>, then <code>inner</code> at top.
DThe call stack has only <code>inner</code> because other functions have returned.
Attempts:
2 left
💡 Hint

Think about the order functions call each other and how the stack grows.

Practice

(1/5)
1. What happens when a function is called in Rust?
easy
A. The program runs the function's code and then returns to the call point.
B. The program skips the function's code and continues after the call.
C. The function code runs only if called twice.
D. The program stops running after the function call.

Solution

  1. Step 1: Understand function call behavior

    When a function is called, the program jumps to the function's code and executes it.
  2. Step 2: Return to the call point

    After the function finishes, the program returns to the line after the call and continues running.
  3. Final Answer:

    The program runs the function's code and then returns to the call point. -> Option A
  4. Quick Check:

    Function call flow = run code then return [OK]
Hint: Function calls run code then return to caller [OK]
Common Mistakes:
  • Thinking function code is skipped
  • Believing program stops after call
  • Assuming function runs only if called twice
2. Which of these is the correct way to call a function named greet in Rust?
easy
A. call greet;
B. greet();
C. greet[];
D. function greet();

Solution

  1. Step 1: Recall Rust function call syntax

    Functions are called by writing their name followed by parentheses, even if no arguments are passed.
  2. Step 2: Match the correct syntax

    Only greet(); matches this pattern correctly.
  3. Final Answer:

    greet(); -> Option B
  4. Quick Check:

    Function call syntax = name + () [OK]
Hint: Use functionName() to call functions in Rust [OK]
Common Mistakes:
  • Omitting parentheses when calling functions
  • Using brackets [] instead of parentheses
  • Using keywords like 'call' or 'function' incorrectly
3. What will be the output of this Rust code?
fn add_one(x: i32) -> i32 {
    x + 1
}

fn main() {
    let result = add_one(5);
    println!("{}", result);
}
medium
A. 5
B. Error: missing semicolon
C. 6
D. 0

Solution

  1. Step 1: Understand the function behavior

    The function add_one takes an integer and returns that integer plus one.
  2. Step 2: Trace the function call and output

    Calling add_one(5) returns 6, which is printed by println!.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    5 + 1 = 6 [OK]
Hint: Follow function return values to find output [OK]
Common Mistakes:
  • Confusing input and output values
  • Thinking semicolon causes error here
  • Assuming function prints instead of returns
4. Identify the error in this Rust code snippet:
fn multiply(x: i32, y: i32) -> i32 {
    x * y
}

fn main() {
    let result = multiply(4);
    println!("{}", result);
}
medium
A. Function multiply is missing a return statement.
B. Variable result is not declared.
C. println! macro is used incorrectly.
D. multiply is called with wrong number of arguments.

Solution

  1. Step 1: Check function definition parameters

    The function multiply requires two arguments: x and y.
  2. Step 2: Check function call arguments

    The call multiply(4) provides only one argument, causing an error.
  3. Final Answer:

    multiply is called with wrong number of arguments. -> Option D
  4. Quick Check:

    Function call args must match definition [OK]
Hint: Match function call arguments to definition [OK]
Common Mistakes:
  • Ignoring argument count mismatch
  • Thinking missing return causes error here
  • Assuming println! usage is wrong
5. Given these functions:
fn first() {
    println!("Start");
    second();
    println!("End");
}

fn second() {
    println!("Middle");
}

fn main() {
    first();
}

What is the order of printed lines when main runs?
hard
A. Start\nMiddle\nEnd
B. Middle\nStart\nEnd
C. Start\nEnd\nMiddle
D. End\nMiddle\nStart

Solution

  1. Step 1: Trace the call flow from main

    Main calls first(), which prints "Start" then calls second().
  2. Step 2: Follow nested function calls

    second() prints "Middle" and returns. Then first() prints "End".
  3. Final Answer:

    Start\nMiddle\nEnd -> Option A
  4. Quick Check:

    Call flow order = Start, Middle, End [OK]
Hint: Follow calls step-by-step to find print order [OK]
Common Mistakes:
  • Ignoring nested function call order
  • Assuming prints happen after all calls
  • Mixing up print sequence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard