Closures with Parameters
Part of the Logic & Flow section of Coddy's Rust journey — lesson 61 of 66.
While closures that take no parameters are useful, most of the time you'll want to pass data into your closures to make them more flexible and powerful. Adding parameters to closures follows the same pattern you learned earlier, but with values inside the vertical bars.
Here's how you define a closure that accepts one parameter:
let add_one = |x: i32| x + 1;Notice that the parameter x goes between the vertical bars, just like function parameters. You can then call this closure by passing a value:
let add_one = |x: i32| x + 1;
let result = add_one(5); // result is 6For closures with multiple parameters, separate them with commas:
let multiply = |x: i32, y: i32| x * y;
let result = multiply(3, 4); // result is 12Challenge
EasyYou will receive two inputs. The first input is a number, and the second input is also a number. Create a closure that takes two parameters and calculates the power (first parameter raised to the second parameter). Store the closure in a variable, call it with the input values, and print the result.
Requirements:
- Read the first input (base number) and trim whitespace
- Parse it to
i32 - Read the second input (exponent) and trim whitespace
- Parse it to
u32 - Create a closure with two parameters that calculates the power using the
.pow()method - Store the closure in a variable
- Call the closure with both input values
- Print the result
Input:
- First line: A base number (e.g.,
2) - Second line: An exponent (e.g.,
3)
Output:
- The result of the power calculation
Cheat sheet
Closures can accept parameters by placing them between the vertical bars ||:
let add_one = |x: i32| x + 1;
let result = add_one(5); // result is 6For multiple parameters, separate them with commas:
let multiply = |x: i32, y: i32| x * y;
let result = multiply(3, 4); // result is 12Try it yourself
use std::io;
fn main() {
// Read the first input (base number)
let mut base_input = String::new();
io::stdin().read_line(&mut base_input).expect("Failed to read line");
let base: i32 = base_input.trim().parse().expect("Invalid number");
// Read the second input (exponent)
let mut exp_input = String::new();
io::stdin().read_line(&mut exp_input).expect("Failed to read line");
let exponent: u32 = exp_input.trim().parse().expect("Invalid number");
// TODO: Create a closure that calculates the power and store it in a variable
// TODO: Call the closure with base and exponent
// Print the result
println!("{}", result);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Control Flow
The 'match' ExpressionMatching Multiple ValuesMatching RangesThe 'if let' ExpressionLoops as ExpressionsRecap - Simple Command Parser4Grouping Data with Structs
What is a Struct?Structs OverviewAccessing Struct FieldsMutable StructsStructs as Function ParametersTuple StructsRecap - Create a Book Struct7Handling Errors with 'Result'
What is a 'Result'?Using 'match' with 'Result'is_ok() and is_err()Shortcuts: unwrap and expectThe Question Mark Operator '?'Parsing Strings to NumbersRecap - Safe Division Function10Closures & Anonymous Functions
What is a Closure?Defining a Simple ClosureClosures with ParametersCapturing the EnvironmentRecap - Simple Adder Closure2Introduction to Vectors
What is a Vector?Creating a VectorAdding Elements with pushAccessing Vector ElementsIterating Over a VectorMutable IterationRemoving ElementsRecap - Basic Score Tracker5Key-Value Pairs with Hash Maps
What is a Hash Map?Creating a Hash MapInserting Key-Value PairsAccessing ValuesIterating Over a Hash MapUpdating a ValueRemoving a PairRecap - Word Counter8Project: Simple Item Inventory
Project SetupAdding an ItemChecking StockSelling an ItemPutting it all together