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 {
//
}
// 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 = |total: &mut f64, discount: f64| { *total = *total - discount; }; // 4. Implement checkout_cart closure here let checkout_cart = move |detail: 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(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 = |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 = |tot_price: &mut f64, discount: f64| {*tot_price -= discount}; // 4. Implement checkout_cart closure here let checkout_cart = move |cart: String| -> 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" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { // 1. Implement calculate_total closure let calculate_total = |price: f64, tax_rate: f64| price + price * tax_rate; // 2. Implement apply_discount closure let apply_discount = |total_price: &mut f64, discount: f64| { *total_price -= discount; }; // 3. Implement checkout_cart closure let checkout_cart = move |cart_details: String| -> String { format!("Checkout complete: {}", cart_details) }; // Return the closures (calculate_total, apply_discount, checkout_cart)}// Example usagepub fn main() { // Create the closures let (calculate_total, mut apply_discount, checkout_cart) = create_typed_closures(); // Test calculate_total assert_eq!(calculate_total(100.0, 0.2), 120.0); // Test apply_discount let mut total_price = 120.0; apply_discount(&mut total_price, 20.0); assert_eq!(total_price, 100.0); // Test checkout_cart let cart_details = String::from("Items: Apple, Banana, Orange"); assert_eq!( checkout_cart(cart_details), "Checkout complete: Items: Apple, Banana, Orange" ); println!("All tests passed!");}
// 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 = |detail: 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_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 = |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 = |cart_total : &mut f64, discount| { *cart_total = *cart_total - 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(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 = |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 = |price: &mut f64, discount| *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(f32,f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f32, rate: f32| { price + (price * rate) }; // 3. Implement apply_discount closure here let apply_discount = |cart: &mut f32, discount: f32| { *cart -= 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(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 -= 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" );}
// 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 = |s| 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 = |total, tax_rate| total + total*tax_rate; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f32, discount| { *total = *total - discount }; // 4. Implement checkout_cart closure here let checkout_cart = |items| { format!("Checkout complete: {}", items) }; (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 = |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 = |cost: f64, tax: f64| { cost + (cost * tax) }; // 3. Implement apply_discount closure here let apply_discount = |cost: &mut f64, discount: f64| { *cost = *cost - 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, amount: f64| { *price -= amount }; // 4. Implement checkout_cart closure here let checkout_cart = |items: String| { let details = format!("Checkout complete: {}", items); 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 = |x,y| {x+x*y}; // 3. Implement apply_discount closure here let apply_discount = |x: &mut f64,y| {*x-=y;}; // 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 = |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_rate| price + price * tax_rate; // 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 = |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: f64, rate: f64| price + price * rate; // 3. Implement apply_discount closure here let apply_discount = |cart: &mut f64, discount: f64| *cart = *cart - 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(f32, f32)-> f32, impl FnMut(&mut f32, f32) -> (), impl FnOnce(String) -> String) { // 2. Implement calculate_total closure here let calculate_total = |item_cost: f32, tax_rate: f32| -> f32 { item_cost + item_cost * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f32, discount: f32| { *total = *total - discount }; // 4. Implement checkout_cart closure here let checkout_cart = |details: String| -> 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(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: f32| { *price -= discount }; // 4. Implement checkout_cart closure here let checkout_cart = |cart: String| { format!("Checkout complete: Items: {}", 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_price: &mut f64, discount: f64| { *total_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" );}
// 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 = |items: String| { format!("Checkout: {}", items) }; (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) { // 2. Implement calculate_total closure here let calculate_total = |x: f32, y: f32| {x + x * y}; // 3. Implement apply_discount closure here let apply_discount = |x: &mut f32, y: f32| {*x -= y}; // 4. Implement checkout_cart closure here let checkout_cart = |x: String| {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(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 = |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" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64), impl FnOnce(String) -> String,) { let calculate_total = |price: f64, tax_rate: f64 | { price + price * tax_rate }; let apply_discount = |total: &mut f64, discount: f64| { *total -= discount }; let checkout_cart = |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: 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 = |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(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f32, taxrate: f32| price + price * taxrate; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f32, discount: f32| *price -= discount; // 4. Implement checkout_cart closure here let checkout_cart = move |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(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String,) { // 2. Implement calculate_total closure here let calculate_total = |price: f32, taxrate: f32| price + price * taxrate; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f32, discount: f32| *price -= discount; // 4. Implement checkout_cart closure here let checkout_cart = move |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(f32, f32) -> f32, impl FnMut(&mut f32, f32), impl FnOnce(String) -> String){ // 2. Implement calculate_total closure here let calculate_total = |price: f32, tax: f32| -> f32 {(price * (tax + 1.0)).floor()}; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f32, discount: f32| {*price = *price - discount}; // 4. Implement checkout_cart closure here let checkout_cart = |cart: String| -> 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(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 = |price:&mut f32,tax_rate:f32| { *price -= tax_rate; }; // 4. Implement checkout_cart closure here let checkout_cart = |content:String| { format!("Checkout complete: {}", content) }; (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" );}