Functions help you organize your code into small, reusable pieces. They make your program easier to read and use.
Function declaration in Rust
Introduction
Syntax
Rust
fn function_name(parameters) -> return_type {
// code to run
}Functions start with the keyword fn.
Parameters are optional. If the function returns a value, use -> return_type.
Examples
Rust
fn greet() {
println!("Hello!");
}Rust
fn add(a: i32, b: i32) -> i32 {
a + b
}Rust
fn is_even(num: i32) -> bool {
num % 2 == 0
}Sample Program
This program defines a function greet that takes a name and prints a greeting. The main function calls greet twice with different names.
Rust
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
greet("Bob");
}Important Notes
Functions can have zero or more parameters.
If a function returns a value, the last expression without a semicolon is the return value.
Use fn main() as the starting point of your Rust program.
Summary
Functions group code to do specific tasks.
Use fn keyword to declare a function.
Functions can take inputs (parameters) and give back outputs (return values).
Practice
1. What keyword is used to declare a function in Rust?
easy
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]
Hint: Remember Rust uses 'fn' to start functions [OK]
Common Mistakes:
- Using 'function' instead of 'fn'
- Confusing with Python's 'def'
- Using 'func' which is not Rust syntax
2. Which of the following is the correct syntax to declare a function named
greet that takes no parameters and returns nothing in Rust?easy
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]
Hint: Functions always need () even if no parameters [OK]
Common Mistakes:
- Omitting parentheses
- Using other language keywords
- Adding unnecessary keywords
3. What will be the output of this Rust code?
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("{}", add(3, 4));
}medium
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]
Hint: Sum of 3 and 4 is 7, printed as output [OK]
Common Mistakes:
- Thinking 'a + b' prints as string
- Concatenating numbers as strings
- Assuming missing return error
4. Identify the error in this Rust function declaration:
fn multiply(x i32, y: i32) -> i32 {
x * y
}medium
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]
Hint: Parameters need ':' between name and type [OK]
Common Mistakes:
- Omitting colon in parameters
- Adding unnecessary semicolons
- Confusing return type syntax
5. How would you declare a Rust function named
is_even that takes an integer parameter and returns a boolean indicating if the number is even?hard
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]
Hint: Use '-> bool' and 'n % 2 == 0' for even check [OK]
Common Mistakes:
- Using wrong type names like int or boolean
- Omitting return type
- Using '=' instead of '==' in condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
