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.
pub fn return_closure() -> impl Fn(f64, f64) -> f64 {
//
}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| { price + price * tax_rate }; let apply_discount = |price: &mut f64, discount| { *price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |details| { format!("Checkout complete: {}", details) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32) -> f32, impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |price, rate| { price + price * rate}; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f32, amount| { *total -= amount; total.clone() }; // 4. Implement checkout_cart closure here let checkout_cart = |details| { format!("Checkout complete: {}", details) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String, ) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| {price + price*tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f32, discount| {*total_price -= discount}; // 4. Implement checkout_cart closure here let checkout_cart = |cart| {format!("Checkout complete: {cart}")}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures()->( impl Fn(f64,f64)->f64, impl FnMut(&mut f64,f64), impl FnOnce(String)->String,) { // 2. Implement calculate_total closure here let calculate_total = |price,tax_rate| { price+price*tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |total:&mut f64,discount| { *total-=discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |text| {format!("Checkout Complete: {}",text)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64,f64) -> f64, impl FnMut(&mut f64,f64), impl FnOnce(String) -> String, ){ // 2. Implement calculate_total closure here let calculate_total = |a,b| {a+a*b}; // 3. Implement apply_discount closure here let apply_discount = |a:&mut f64,b:f64| {*a -= b}; // 4. Implement checkout_cart closure here let checkout_cart = |a| {format!("Checkout Complete: {}",a)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |total: f64, tax_percentage: f64| total + (total * tax_percentage); // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount: f64| *total -= discount; // 4. Implement checkout_cart closure here let checkout_cart = |message: String| -> String { message.replace("Items:", "Checkout complete:") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> (impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |price, tax| { price + price*tax }; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f32, discount| { *total_price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |instr: String| -> String { instr.replace("Items:", "Checkout complete:") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| -> f32 { price + tax_rate * price }; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f32, discount| { *total_price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |instr: String| -> String { instr.replace("Items:", "Checkout complete:") }; let original_string = "Hello World! This is a World of Rust."; let replaced_string = original_string.replace("World", "Rustacean"); println!("Original: {}", original_string); println!("Replaced: {}", replaced_string); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a, b| a + a * b; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f32, b| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = move |s| format!("Checkout complete:{}", s); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> (impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String, ) { // 2. Implement calculate_total closure here let calculate_total = |a, b| { a + a*b }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f32, b: f32| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = move |a| -> String { format!("Checkout complete: {}", a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures()->( impl Fn (f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String)->String,){ // 2. Implement calculate_total closure here let calculate_total = |price:f32,tax_rate:f32| {price + price*tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |total:&mut f32, discount:f32| {*total -=discount}; // 4. Implement checkout_cart closure here let checkout_cart = |cart_details:String| {format!("Checkout complete: {}",cart_details)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures()->( impl Fn (f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String)->String,){ // 2. Implement calculate_total closure here let calculate_total = |price:f32,tax_rate:f32| {price + price*tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |total:&mut f32, discount:f32| {*total -=discount}; // 4. Implement checkout_cart closure here let checkout_cart = move |cart_details:String| {format!("Checkout complete: {}",cart_details)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures()->( impl Fn(f64,f64)->f64, impl FnMut(&mut f64,f64), impl FnOnce(String)->String) { // 2. Implement calculate_total closure here let calculate_total = |p,t| { return p + (p*t); }; // 3. Implement apply_discount closure here let apply_discount = |price:&mut f64,discount| { *price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |details| { format!("Checkout complete: {}",details) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount| { *price = *price - discount }; // 4. Implement checkout_cart closure here let checkout_cart = |cart| { format!("Checkout complete: {cart}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> (impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |a,b| { a + a*b}; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b| { *a = *a - b; }; // 4. Implement checkout_cart closure here let checkout_cart = |cart| { format!("Checkout complete: {cart}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |total: &mut f32, discount| *total -= discount; let checkout_cart = |cart_details| format!("Checkout complete: {cart_details}"); (calculate_total, apply_discount, checkout_cart)}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |a: f64, b: f64| a + a * b; // 3. Implement apply_discount closure here let apply_discount = |p: &mut f64, d: f64| *p -= d; // 4. Implement checkout_cart closure here let checkout_cart = |s: String| format!("Checkout complete: {}", s).to_string() ; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate ; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount| *total -= discount; // 4. Implement checkout_cart closure here let checkout_cart = |detail| {format!("Checkout complete: {detail}")}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String ) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate ; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f32, discount| *total -= discount; // 4. Implement checkout_cart closure here let checkout_cart = |detail| {format!("Checkout complete: {detail}")}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl for<'a> Fn(&'a mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a, b| { a + a * b }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = |a| { format!("Checkout complete: {}", a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!(checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange");}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64,f64)->f64, impl FnMut(&mut f64, f64)->() , impl FnOnce(String) -> String ) { // 2. Implement calculate_total closure here let calculate_total = |a: f64,b: f64| { a + a*b }; // 3. Implement apply_discount closure here let apply_discount = | a: &mut f64, b| { *a= *a-b;}; // 4. Implement checkout_cart closure here let checkout_cart = move |x| { format!("Checkout complete: Items: {}", x) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() ->(impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,){ // 2. Implement calculate_total closure here let calculate_total = |x,y| { x+x*y }; // 3. Implement apply_discount closure here let apply_discount = |x:&mut f64,y:f64| { *x-=y; }; // 4. Implement checkout_cart closure here let checkout_cart = |s: String| { format!("Checkout complete: {}", s) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String, ){ // 2. Implement calculate_total closure here let calculate_total = |a, b| a + (a * b); // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b: f64| {*a -= b}; // 4. Implement checkout_cart closure here let checkout_cart = |a| {format!("Checkout complete: {}", a)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f64, tax: f64| price + price * tax; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount: f64| *price -= discount; // 4. Implement checkout_cart closure here let checkout_cart = |cart: String| format!("Checkout complete: {cart}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64)->f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String){ // 2. Implement calculate_total closure here let calculate_total = |price: f64, tax_rate: f64| { price + price*tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount: f64| { *price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |cart: String| { format!("Checkout complete: {cart}") }; (calculate_total, apply_discount, checkout_cart)} // Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a: f64, b: f64| { a.mul_add(b, a) }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b: f64| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = |a: String| { format!("Checkout complete: {}", a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |a: f64, b: f64| { a.mul_add(b, a) }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b: f64| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = |a| { format!("Checkout complete: {}", a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |a: f64, b: f64| { a.mul_add(b, a) }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f64, b: f64| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = |a| { format!("Checkout complete: {}", a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String ) { // 2. Implement calculate_total closure here let calculate_total = |price, tax| { price + price * tax }; // 3. Implement apply_discount closure here let apply_discount =|cart: &mut f64, discount| { *cart -= discount;}; // 4. Implement checkout_cart closure here let checkout_cart = |s| {format!("Checkout complete: {s}")}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a, b| { a + a * b }; // 3. Implement apply_discount closure here let apply_discount = |a: &mut f32, b| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = |a| { format!("Checkout complete: {}", &a) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String){ // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount| { *total -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = move |s| { format!("Checkout complete: {}", s) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64,f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax| { price + price * tax }; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount: f64| { *total -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |carts: String| -> String { format!("Checkout complete: {carts}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64 , discount| { *price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |details| format!("Checkout complete: {}", details); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate; // 3. Implement apply_discount closure here let apply_discount = |sum: &mut f64, discount| { *sum -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |details| format!("Checkout complete {details}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |total_price: &mut f32, discount: f32| { *total_price -= discount }; let checkout_cart = |cart_details| { format!("Checkout complete: Items: {cart_details}") }; (calculate_total, apply_discount, checkout_cart)}pub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount: f64| *total -= discount ; // 4. Implement checkout_cart closure here let checkout_cart = move |details: String| format!("Checkout complete: {details}").to_string(); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64) -> (), impl FnOnce(String) -> String,){ // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| {price + price * tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount: f64| { *price -= discount}; // 4. Implement checkout_cart closure here let checkout_cart = |cart_string: String| { // "Checkout complete: Items: ".to_string() + cart.join(", ") String::from("Checkout complete: ".to_string() + &cart_string) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax_rate| price + price * tax_rate; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f64, discount| *total_price -= discount ; // 4. Implement checkout_cart closure here let checkout_cart = |cart| format!("checkout: {cart}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |total_price: &mut f64, discount| *total_price -= discount; let checkout_cart = |cart| format!("Checkout done: {cart}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |total_price: &mut f64, discount| *total_price -= discount; let checkout_cart = |cart| format!("Checkout done: {cart}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String){ // 2. Implement calculate_total closure here let calculate_total = |price: f64, tax_rate: f64| { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount: f64| { *price -= discount }; // 4. Implement checkout_cart closure here let checkout_cart = move |cart: String| { format!("checkout done : {cart}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String ) { // 2. Implement calculate_total closure here let calculate_total = |a: f64, b: f64| { a + a * b }; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discount: f64| { *price -= discount }; // 4. Implement checkout_cart closure here let checkout_cart = move |cart: String| { format!("checkout done : {cart}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f64, tax_rate: f64| { price + price * tax_rate}; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f64, discount: f64| { *total_price -= discount }; // 4. Implement checkout_cart closure here let checkout_cart = | details: String | { format!("Checkout complete: {}", details)}; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f64, tax_rate: f64| price + price * tax_rate; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount: f64| { *total -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = move |detail: String| -> String { format!("Checkout complete: {}", detail) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price, tax| price + price * tax; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, price| *total -= price; // 4. Implement checkout_cart closure here let checkout_cart = move |value| format!("Checkout complete: {value}").to_string(); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |cart_total: &mut f64, discount: f64| *cart_total -= discount; let checkout_cart = |details| format!("Checkout complete: {details}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a, b| a + a * b; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f64, discount: f64| *total = *total - discount; // 4. Implement checkout_cart closure here let checkout_cart = |details: String| format!("Checkout complete: {details}"); (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64) -> (), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |a, b| { a + a * b }; // 3. Implement apply_discount closure here let apply_discount = |a: & mut f64, b| { *a -= b; }; // 4. Implement checkout_cart closure here let checkout_cart = move |s| { format!("Checkout complete: {}", s) }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
// 1. Based on the `main` function below,// Find out the types of the closures and define thempub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = | price, tax_rate | { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = | price: &mut f32, discount | { *price = *price - discount; }; // 4. Implement checkout_cart closure here let checkout_cart = | cart_details | { format!("Checkout complete: {cart_details}") }; (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Example tests assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price, tax_rate| price + price * tax_rate; let apply_discount = |total_price: &mut f64, discount| { *total_price -= discount; }; let checkout_cart = |cart_details| format!("Checkout complete: {cart_details}"); (calculate_total, apply_discount, checkout_cart)}pub fn main() { let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); assert_eq!(calculate_total(100.0, 0.2), 120.0); let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" );}