Function parameters let you send information into a function so it can use that information to do its job.
Function parameters in Rust
Introduction
Syntax
Rust
fn function_name(parameter1: Type1, parameter2: Type2) -> ReturnType {
// function body
}Parameters are listed inside parentheses after the function name.
Each parameter has a name and a type separated by a colon.
Examples
name which is a string slice.Rust
fn greet(name: &str) {
println!("Hello, {}!", name);
}Rust
fn add(x: i32, y: i32) -> i32 {
x + y
}Rust
fn print_number(num: u32) {
println!("Number: {}", num);
}Sample Program
This program defines a function greet that takes a name and prints a greeting. In main, it calls greet twice with different names.
Rust
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
greet("Bob");
}Important Notes
Parameters are like placeholders for values you give when calling the function.
Rust requires you to specify the type of each parameter.
You can have multiple parameters separated by commas.
Summary
Function parameters let you send data into functions.
Each parameter has a name and a type.
Use parameters to make functions flexible and reusable.
Practice
1. What is the purpose of function parameters in Rust?
easy
Solution
Step 1: Understand what parameters do
Parameters allow functions to receive input data when called.Step 2: Identify the correct purpose
Sending data into functions makes them flexible and reusable.Final Answer:
To send data into functions so they can use it -> Option AQuick Check:
Function parameters = input data [OK]
Hint: Parameters pass data into functions for flexibility [OK]
Common Mistakes:
- Confusing parameters with return types
- Thinking parameters declare variables inside functions
- Mixing parameters with control flow like loops
2. Which of the following is the correct way to declare a function with one parameter named
num of type i32 in Rust?easy
Solution
Step 1: Recall Rust function parameter syntax
Parameters are declared asname: type, e.g.,num: i32.Step 2: Check each option
fn example(num: i32) {} matches correct syntax. fn example(i32 num) {} reverses order. fn example(num) -> i32 {} misses type. fn example(num: int) {} uses wrong type name.Final Answer:
fn example(num: i32) {} -> Option CQuick Check:
Parameter syntax = name: type [OK]
Hint: Use name: type for parameters in Rust functions [OK]
Common Mistakes:
- Swapping type and name order
- Omitting the parameter type
- Using incorrect type names like 'int'
3. What will be the output of this Rust code?
fn greet(name: &str) {
println!("Hello, {}!", name);
}
fn main() {
greet("Alice");
}medium
Solution
Step 1: Understand the function call and parameter
The functiongreettakes a string slice&strand prints a greeting with the given name.Step 2: Trace the function call in
Callingmaingreet("Alice")passes "Alice" as the parameter, so it prints "Hello, Alice!".Final Answer:
Hello, Alice! -> Option AQuick Check:
Function prints greeting with passed name [OK]
Hint: Parameters replace placeholders in print statements [OK]
Common Mistakes:
- Printing the parameter name instead of its value
- Confusing &str with variable name
- Expecting a compilation error due to &str
4. Identify the error in this Rust function declaration:
fn add(a i32, b: i32) -> i32 {
a + b
}medium
Solution
Step 1: Check parameter syntax
Each parameter must have a colon between name and type, e.g.,a: i32.Step 2: Identify the error
Parametera i32is missing the colon, causing a syntax error.Final Answer:
Missing colon between parameter name and type for 'a' -> Option DQuick Check:
Parameter syntax requires colon [OK]
Hint: Always put colon between parameter name and type [OK]
Common Mistakes:
- Omitting colon between name and type
- Confusing parameter order syntax
- Forgetting return type after ->
5. Given this function:
Which call correctly uses the function parameters to calculate the area of a 5.0 by 3.0 rectangle?
fn calculate_area(width: f64, height: f64) -> f64 {
width * height
}Which call correctly uses the function parameters to calculate the area of a 5.0 by 3.0 rectangle?
hard
Solution
Step 1: Check parameter types
The function expects twof64(floating-point) parameters.Step 2: Evaluate each call
calculate_area(5, 3) uses integer literals, which Rust automatically coerces tof64(implicit conversion). calculate_area(5.0, 3.0) uses floating-point literals matching thef64parameter type exactly. calculate_area("5", "3") uses strings, type mismatch. calculate_area(width=5.0, height=3.0) uses named arguments, which Rust does not support.Final Answer:
calculate_area(5.0, 3.0) -> Option BQuick Check:
Use correct types and order for parameters [OK]
Hint: Match argument types exactly to parameter types [OK]
Common Mistakes:
- Using integers instead of floats without suffix
- Passing strings instead of numbers
- Trying named arguments (not supported in Rust)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
