Closure Types
Closures in Rust are categorized based on how they interact with the environment and the variables they capture. The three main types are Fn, FnMut, and FnOnce. Understanding and using these types effectively is key to mastering closures in Rust.
Closure Type Definitions
- Fn: A closure that borrows its environment immutably. It is useful for read-only operations.
- FnMut: A closure that mutably borrows its environment, allowing it to modify variables.
- FnOnce: A closure that takes ownership of its captured variables, consuming them in the process.
Your Task
Implement the following closures and their respective behaviors:
calculate_total: AnFnclosure that calculates the total price of an item, including tax (price + price * tax_rate).apply_discount: AnFnMutclosure that mutates the cart total by subtracting a given discount.checkout_cart: AnFnOnceclosure that consumes the cart's details (aString) and returns a confirmation message.
Hints
Click here to reveal hints
- Use
impl Fn,impl FnMut, orimpl FnOnceto define the types of the closures. e.g.rust pub fn return_closure() -> impl Fn(f64, f64) -> f64 { // }
No run yet
Press
Run to execute your code and see test results here.