A return value is the result a function sends back to the place where it was called. It lets the function give back information after doing its work.
Click to reveal answer
beginner
How do you specify a return type in a Rust function?
You write an arrow (->) after the function parameters, followed by the type of the value the function will return.
Click to reveal answer
beginner
What happens if a Rust function does not have a return statement?
If there is no return statement, the function returns the unit type (), which means it returns nothing useful.
Click to reveal answer
intermediate
Can Rust functions return multiple values directly?
Rust functions return a single value, but you can return multiple values by using a tuple to group them together.
Click to reveal answer
intermediate
What is the difference between using 'return' and an expression at the end of a Rust function?
Using 'return' immediately sends back a value and ends the function. Writing an expression without a semicolon at the end also returns its value, but it's more idiomatic in Rust.
Click to reveal answer
How do you indicate the return type of a function in Rust?
AUsing '->' followed by the type after the parameters
BUsing ':' before the function name
CUsing 'return' keyword before the function name
DUsing '=>' after the function body
✗ Incorrect
In Rust, the return type is specified with '->' after the function parameters.
What does a Rust function return if it has no return statement and no expression at the end?
AAn error
BNull
CZero
DThe unit type '()'
✗ Incorrect
If no return value is specified, Rust functions return the unit type '()' which means no meaningful value.
Which of these is a valid way to return a value from a Rust function?
AOnly using 'value;' with a semicolon
BOnly using 'return value;'
CUsing 'return value;' or just 'value' as the last expression
DUsing 'print(value);'
✗ Incorrect
Rust allows returning a value either with 'return value;' or by having the value as the last expression without a semicolon.
How can you return multiple values from a Rust function?
ABy returning multiple values separated by commas
BBy returning a tuple containing all values
CBy returning an array of values
DRust functions cannot return multiple values
✗ Incorrect
Rust functions return a single value, but you can group multiple values in a tuple and return that.
What does the following Rust function return?
fn example() -> i32 {
5
}
A5
BNothing
CAn error
DUnit type ()
✗ Incorrect
The function returns 5 because the last expression '5' without a semicolon is the return value.
Explain how return values work in Rust functions and how to specify them.
Think about the arrow syntax and how Rust treats the last expression.
You got /4 concepts.
Describe how you can return multiple pieces of information from a Rust function.
Rust functions return one value, but that value can hold many things.
You got /3 concepts.
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
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 D
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
Step 1: Check Rust return syntax
Rust uses -> Type to specify return type after function parentheses.
Step 2: Validate options
fn add() -> i32 { 5 + 3 } uses -> i32 correctly; others use invalid syntax or wrong type name.
Final Answer:
fn add() -> i32 { 5 + 3 } -> Option B
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
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 A
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
Step 1: Check return statement
The function declares return type i32 but uses return; 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 C
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?