What if your functions could hand you answers like a helpful friend instead of just talking at you?
Why Return values in Rust? - Purpose & Use Cases
Imagine you are baking cookies and want to share the recipe results with your friend. You write down the steps but forget to tell them how many cookies the recipe makes. They have to guess or bake blindly.
Without return values, your program can't send back results after doing work. This means you must rely on extra steps like printing or global variables, which is slow, confusing, and easy to mess up.
Return values let functions hand back results directly. This makes your code clear and efficient, like giving your friend the exact cookie count after baking.
fn add(a: i32, b: i32) { println!("{}", a + b); }fn add(a: i32, b: i32) -> i32 { a + b }Return values let your programs pass results smoothly, enabling complex calculations and data flow.
When calculating a shopping total, a function returns the sum so you can use it to show the price or apply discounts next.
Return values send results back from functions.
They avoid messy workarounds like printing inside functions.
They help build clear, reusable code that flows logically.
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
