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.
Implement the following closures and their respective behaviors:
calculate_total
: An Fn
closure that calculates the total price of an item, including tax (price + price * tax_rate
).apply_discount
: An FnMut
closure that mutates the cart total by subtracting a given discount.checkout_cart
: An FnOnce
closure that consumes the cart's details (a String
) and returns a confirmation message.impl Fn
, impl FnMut
, or impl FnOnce
to define the types of the closures. e.g.