Bird
Raised Fist0
Rustprogramming~10 mins

Return values in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Return values
Start function
↓
Execute statements
↓
Evaluate return expression
↓
Return value to caller
↓
Function ends
This flow shows how a Rust function runs its code and then sends a value back to where it was called.
Execution Sample
Rust
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let result = add(2, 3);
    println!("{}", result);
}
This code defines a function that adds two numbers and returns the sum, then prints the result.
Execution Table
StepActionExpression EvaluatedReturn ValueOutput
1Call add(2, 3)N/AN/AN/A
2Evaluate a + b2 + 35N/A
3Return 5 to caller55N/A
4Assign return value to resultresult = 55N/A
5Print resultprintln!("{}", 5)55
💡 Function returns after evaluating the expression 'a + b', returning 5 to the caller.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
aN/A2N/AN/A
bN/A3N/AN/A
resultN/AN/A55
Key Moments - 2 Insights
Why does the function return 5 without using the 'return' keyword?
In Rust, the last expression without a semicolon is automatically returned, as shown in step 2 and 3 of the execution_table.
What happens if we add a semicolon after 'a + b' inside the function?
Adding a semicolon turns the expression into a statement, so the function returns '()', the unit type, not the sum. This is why the return value is evaluated at step 2 without a semicolon.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value at step 3?
A5
B2
C3
DN/A
💡 Hint
Check the 'Return Value' column at step 3 in the execution_table.
At which step is the variable 'result' assigned its value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for when 'result' is assigned.
If we add a semicolon after 'a + b' in the function, what would the return value be?
A5
BCompilation error
CUnit type '()'
D0
💡 Hint
Refer to the key_moments explanation about semicolon effect on return values.
Concept Snapshot
Rust functions return the value of their last expression without needing 'return'.
Add a semicolon to make it a statement, which returns '()' instead.
Use '-> Type' to specify return type.
Returned value goes back to the caller and can be stored or used.
Example: fn add(a: i32, b: i32) -> i32 { a + b }
Full Transcript
This lesson shows how Rust functions return values. When a function runs, it executes its code and then sends back a value to where it was called. In Rust, the last expression in a function without a semicolon is automatically returned. For example, in the add function, the expression 'a + b' is evaluated and returned. The returned value is then assigned to a variable or used directly. Adding a semicolon after the last expression changes it to a statement, causing the function to return the unit type '()' instead of the expected value. This behavior helps write concise functions without explicit return statements.

Practice

(1/5)
1. What does the return value of a Rust function represent?
easy
A. The type of variables inside the function
B. The name of the function
C. The number of lines in the function
D. The result the function sends back to the caller

Solution

  1. Step 1: Understand function purpose

    A function processes data and often produces a result.
  2. Step 2: Identify return value role

    The return value is the output sent back to where the function was called.
  3. Final Answer:

    The result the function sends back to the caller -> Option D
  4. Quick Check:

    Return value = function output [OK]
Hint: Return value is what function gives back [OK]
Common Mistakes:
  • Confusing return value with function name
  • Thinking return value is the number of lines
  • Mixing return value with variable types
2. Which of the following is the correct way to declare a function in Rust that returns an i32 value?
easy
A. fn add() returns i32 { 5 + 3 }
B. fn add() -> i32 { 5 + 3 }
C. fn add() : i32 { 5 + 3 }
D. fn add() -> int { 5 + 3 }

Solution

  1. Step 1: Check Rust return syntax

    Rust uses -> Type to specify return type after function parentheses.
  2. Step 2: Validate options

    fn add() -> i32 { 5 + 3 } uses -> i32 correctly; others use invalid syntax or wrong type name.
  3. Final Answer:

    fn add() -> i32 { 5 + 3 } -> Option B
  4. Quick Check:

    Return type syntax = -> Type [OK]
Hint: Use -> Type after () to declare return type [OK]
Common Mistakes:
  • Using colon : instead of -> for return type
  • Writing 'returns' keyword incorrectly
  • Using 'int' instead of 'i32' in Rust
3. What is the output of this Rust code?
fn multiply(x: i32, y: i32) -> i32 {
    x * y
}

fn main() {
    println!("{}", multiply(4, 5));
}
medium
A. 20
B. 45
C. 9
D. Compilation error

Solution

  1. Step 1: Understand function multiply

    It returns the product of x and y without explicit return keyword.
  2. Step 2: Calculate multiply(4, 5)

    4 * 5 = 20, so the function returns 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    4 * 5 = 20 [OK]
Hint: Last expression is returned automatically [OK]
Common Mistakes:
  • Expecting explicit return keyword
  • Confusing multiplication with addition
  • Thinking code causes error due to missing return
4. Identify the error in this Rust function that is supposed to return an i32:
fn get_number() -> i32 {
    let x = 10;
    return;
}
medium
A. Variable x is unused
B. Function should not have a return type
C. Missing return value after return keyword
D. Function should use println! instead of return

Solution

  1. Step 1: Check return statement

    The function declares return type i32 but uses return; without value.
  2. Step 2: Understand Rust return rules

    Functions with return type must return a value of that type; missing value causes error.
  3. Final Answer:

    Missing return value after return keyword -> Option C
  4. Quick Check:

    Return type requires value [OK]
Hint: Return type needs a value after return [OK]
Common Mistakes:
  • Omitting return value when return type is specified
  • Ignoring unused variable warnings
  • Confusing return with print statements
5. Given this Rust function, what will be the output?
fn check_value(x: i32) -> bool {
    if x > 0 {
        true
    } else {
        false
    }
}

fn main() {
    println!("{}", check_value(-3));
}
hard
A. false
B. Compilation error
C. 0
D. true

Solution

  1. Step 1: Analyze function check_value

    Returns true if x > 0, else false.
  2. Step 2: Evaluate check_value(-3)

    -3 is not greater than 0, so function returns false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    x > 0 ? true : false = false [OK]
Hint: If condition returns last expression automatically [OK]
Common Mistakes:
  • Assuming negative numbers return true
  • Expecting numeric output instead of bool
  • Thinking missing return causes error
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