Function declaration in Rust - Time & Space Complexity
When we declare a function, we want to know how much time it takes to run when called.
We ask: How does the work inside the function grow as input changes?
Analyze the time complexity of the following code snippet.
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
}
This code declares a simple function that prints a greeting message once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single print statement inside the function.
- How many times: Exactly once each time the function is called.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | 1 print operation |
| 10 | 10 print operations if called 10 times |
| 100 | 100 print operations if called 100 times |
Pattern observation: Each call does the same small amount of work, so time grows directly with how many times you call it.
Time Complexity: O(1)
This means the function does a fixed amount of work every time, no matter the input size.
[X] Wrong: "Declaring a function takes time proportional to input size."
[OK] Correct: Declaring a function just sets up the code; it doesn't run it or repeat work. Time is spent only when the function runs.
Understanding that function declarations themselves don't cost time helps you focus on what really matters: the work inside functions when they run.
"What if the function had a loop inside that runs n times? How would the time complexity change?"
Practice
Solution
Step 1: Recall Rust function syntax
In Rust, functions are declared using the keywordfn.Step 2: Compare options with Rust syntax
Only fn matches the correct Rust keyword for function declaration.Final Answer:
fn -> Option AQuick Check:
Function declaration keyword = fn [OK]
- Using 'function' instead of 'fn'
- Confusing with Python's 'def'
- Using 'func' which is not Rust syntax
greet that takes no parameters and returns nothing in Rust?Solution
Step 1: Check function declaration syntax
In Rust, a function with no parameters and no return value is declared asfn name() {}.Step 2: Validate each option
fn greet() {} matches Rust syntax exactly. Options A and B use keywords from other languages. fn greet {} misses parentheses.Final Answer:
fn greet() {} -> Option BQuick Check:
Empty parameter list requires () in Rust [OK]
- Omitting parentheses
- Using other language keywords
- Adding unnecessary keywords
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("{}", add(3, 4));
}Solution
Step 1: Understand the add function
The function takes two integers and returns their sum using expressiona + b.Step 2: Trace the main function call
Callingadd(3, 4)returns 7, which is printed byprintln!.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Thinking 'a + b' prints as string
- Concatenating numbers as strings
- Assuming missing return error
fn multiply(x i32, y: i32) -> i32 {
x * y
}Solution
Step 1: Check parameter syntax
Each parameter must have a colon between name and type. Here,x i32misses the colon.Step 2: Verify other parts
Return type is present, function name is valid, and semicolon is not needed after expression in Rust.Final Answer:
Missing colon after parameter x -> Option CQuick Check:
Parameters need ':' between name and type [OK]
- Omitting colon in parameters
- Adding unnecessary semicolons
- Confusing return type syntax
is_even that takes an integer parameter and returns a boolean indicating if the number is even?Solution
Step 1: Check parameter and return types
Parameter type must bei32and return typeboolin Rust.Step 2: Validate function body syntax
Expressionn % 2 == 0correctly returns a boolean. fn is_even(n: i32) -> bool { n % 2 == 0 } uses correct syntax and types.Final Answer:
fn is_even(n: i32) -> bool { n % 2 == 0 } -> Option DQuick Check:
Use i32 and bool with correct expression [OK]
- Using wrong type names like int or boolean
- Omitting return type
- Using '=' instead of '==' in condition
Ready to prove it?
Pass the test and this pattern moves unknown
