Return values let a function send back a result after it finishes. This helps you reuse work and get answers from functions.
Return values in Rust
Introduction
Syntax
Rust
fn function_name() -> ReturnType {
// code
return value;
}The arrow -> shows the type of value the function will send back.
You can omit the return keyword if the last line is the value to return.
Examples
Rust
fn add() -> i32 {
return 5 + 3;
}return keyword.Rust
fn greet() -> String { "Hello".to_string() }
Rust
fn is_even(num: i32) -> bool {
num % 2 == 0
}Sample Program
This program defines a function that multiplies two numbers and returns the result. The main function calls it and prints the answer.
Rust
fn multiply(x: i32, y: i32) -> i32 {
x * y
}
fn main() {
let result = multiply(4, 5);
println!("4 times 5 is {}", result);
}Important Notes
Functions must declare the type of value they return.
The last expression without a semicolon is the return value.
Using return is optional but can help with clarity.
Summary
Return values let functions send results back to where they were called.
Use -> Type to specify what type a function returns.
The last line of a function can be the return value without return.
Practice
1. What does the return value of a Rust function represent?
easy
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]
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
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]
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
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]
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
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]
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
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]
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
10 questions ~5 mineasy hard
