Return values in Rust - Time & Space Complexity
We want to understand how the time it takes to get a return value changes as the input grows.
How does the program's work increase when returning values from functions?
Analyze the time complexity of the following code snippet.
fn sum_array(arr: &[i32]) -> i32 {
let mut total = 0;
for &num in arr {
total += num;
}
total
}
This function adds all numbers in an array and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the array.
- How many times: Once for every element in the input array.
As the array gets bigger, the function does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to get the return value grows in a straight line with the input size.
[X] Wrong: "Returning a value is always instant and does not depend on input size."
[OK] Correct: Sometimes returning a value means doing work first, like adding all numbers, so time depends on input size.
Understanding how return values affect time helps you explain your code clearly and shows you know how programs grow with input.
"What if the function returned the maximum number instead of the sum? How would the time complexity change?"
Practice
Solution
Step 1: Understand function purpose
A function processes data and often produces a result.Step 2: Identify return value role
The return value is the output sent back to where the function was called.Final Answer:
The result the function sends back to the caller -> Option DQuick Check:
Return value = function output [OK]
- Confusing return value with function name
- Thinking return value is the number of lines
- Mixing return value with variable types
i32 value?Solution
Step 1: Check Rust return syntax
Rust uses-> Typeto specify return type after function parentheses.Step 2: Validate options
fn add() -> i32 { 5 + 3 } uses-> i32correctly; others use invalid syntax or wrong type name.Final Answer:
fn add() -> i32 { 5 + 3 } -> Option BQuick Check:
Return type syntax = -> Type [OK]
- Using colon : instead of -> for return type
- Writing 'returns' keyword incorrectly
- Using 'int' instead of 'i32' in Rust
fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
println!("{}", multiply(4, 5));
}Solution
Step 1: Understand function multiply
It returns the product of x and y without explicit return keyword.Step 2: Calculate multiply(4, 5)
4 * 5 = 20, so the function returns 20.Final Answer:
20 -> Option AQuick Check:
4 * 5 = 20 [OK]
- Expecting explicit return keyword
- Confusing multiplication with addition
- Thinking code causes error due to missing return
i32:fn get_number() -> i32 {
let x = 10;
return;
}Solution
Step 1: Check return statement
The function declares return typei32but usesreturn;without value.Step 2: Understand Rust return rules
Functions with return type must return a value of that type; missing value causes error.Final Answer:
Missing return value after return keyword -> Option CQuick Check:
Return type requires value [OK]
- Omitting return value when return type is specified
- Ignoring unused variable warnings
- Confusing return with print statements
fn check_value(x: i32) -> bool {
if x > 0 {
true
} else {
false
}
}
fn main() {
println!("{}", check_value(-3));
}Solution
Step 1: Analyze function check_value
Returns true if x > 0, else false.Step 2: Evaluate check_value(-3)
-3 is not greater than 0, so function returns false.Final Answer:
false -> Option AQuick Check:
x > 0 ? true : false = false [OK]
- Assuming negative numbers return true
- Expecting numeric output instead of bool
- Thinking missing return causes error
Ready to prove it?
Pass the test and this pattern moves unknown
