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 = |x, y| x + (x * y); // 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 = |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" ); // println!("{cart_details}");}
// 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, 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 = |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 = |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 = |p : f64,t : f64| -> f64 { return p + p*t; }; // 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 = |items : String| -> String { return 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(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 = |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| { 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 = |total_price : &mut f32, amount| { *total_price -= amount; }; // 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 = |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: 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 = |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: 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" );}
pub fn create_typed_closures() -> ( impl Fn(f32, f32) -> f32, impl FnMut(&mut f32, f32) -> (), impl FnOnce(String) -> String,) { let calculate_total = |a, b| a + a * b; let apply_discount = |a: &mut f32, b: f32| *a -= b; 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" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64) -> (), impl FnOnce(String) -> String,) { let calculate_total = |a, b| a + a * b; let apply_discount = |a: &mut f64, b: f64| *a -= b; 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" );}
pub fn create_typed_closures() -> ( impl Fn(f64, f64) -> f64, impl FnMut(&mut f64, f64) -> (), impl FnOnce(String) -> String,) { let calculate_total = |a, b| a + a * b; let apply_discount = |a: &mut f64, b: f64| *a -= b; 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(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| -> f32 { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f32, discount: f32| *total_price -= discount; // 4. Implement checkout_cart closure here let checkout_cart = |cart_details: String| "Checkout complete: ".to_string() + &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 = |a| { format!("Check 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 = | x, y | { x + x*y }; // 3. Implement apply_discount closure here let apply_discount = | x: &mut f64, discount | { *x -= discount }; // 4. Implement checkout_cart closure here let checkout_cart = | x | { format!("Checkout complete: {}", 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 = |price: f64, tax_rate: f64| -> f64 { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |price: &mut f64, discout: f64| { *price -= discout; }; // 4. Implement checkout_cart closure here let checkout_cart = |cart_details: String| { format!("Checkout complete: Items: {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 = |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 = |a: f64, b: f64| 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 = move |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 = |price: f64, tax: f64| -> f64 { 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 = |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 = |cost: &mut f64, discount: f64| { *cost -= 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| -> f32 { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |cart_total: &mut f32, discount: f32| { *cart_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(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 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: f64| { *total -= 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: f64| { *total -= 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_price: &mut f64, discount| { *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| -> f32 { price + price * tax_rate }; // 3. Implement apply_discount closure here let apply_discount = |total_price: &mut f32, discount: f32| { *total_price = *total_price - discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |cart_details: String| -> 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= |price, tax| {price + price*tax}; // 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_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(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 = |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 = |price, tax_rate| { return 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_details| { format!("Checkout complete: Items: {}", 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 = |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 = |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| *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, 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: String| "Checkout complete".to_string() + a.as_str(); (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 = |details: String| { let mut result = "Checkout complete: ".to_string(); result.push_str(details.as_str()); result }; (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: f32, pourcentage: f32| { total + total * pourcentage }; // 3. Implement apply_discount closure here let apply_discount = |total: &mut f32, pourcentage: f32| { *total = *total - pourcentage }; // 4. Implement checkout_cart closure here let checkout_cart = |cart_detail: String| { format!("Checkout complete: {}", cart_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: f32, tax_rate: f32| { let total = price + price * tax_rate; total }; // 3. Implement apply_discount closure here let apply_discount = |price: & mut f32, discount_rate: f32| { *price = *price - discount_rate; }; // 4. Implement checkout_cart closure here let checkout_cart = |cart: String| { let checkout_message = format!("Checkout complete: {}", cart); checkout_message }; (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 = |a, b| a + a * b; // 3. Implement apply_discount closure here let apply_discount = |t: &mut f32, d| *t -= d; // 4. Implement checkout_cart closure here let checkout_cart = |t| format!("Checkout complete: {}", t); (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|->f64 { p+p*t}; // 3. Implement apply_discount closure here let apply_discount = |p:&mut f64, c:f64| { *p=*p-c;}; // 4. Implement checkout_cart closure here let checkout_cart = |itm| {format!("Checkout complete:{}", itm)}; (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| -> f64 { price + price * tax_rate }; let apply_discount = |total_price: &mut f64, discount: f64| { *total_price -= discount; }; let checkout_cart = move |cart_details: String| -> 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, 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 = |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 = |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 = |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: f32| { *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(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 = |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(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 = |price: &mut f32,discount|{ *price -= discount; }; // 4. Implement checkout_cart closure here let checkout_cart = |d| { format!("Checkout complete: {}",d) }; (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 = |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(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 = |my_str: String| { let my_new_str = "Checkout complete: ".to_string(); my_new_str + &my_str }; (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 = |my_str: String| -> String { let my_new_str = "Checkout complete: ".to_string(); my_new_str + &my_str }; (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 = |my_str: String| -> String { let my_new_str = "Checkout complete: ".to_string(); my_new_str + &my_str }; (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| -> f64 { 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 = |cart_details: String| -> 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 = |price: f64, tax: f64| -> f64 { 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 = |cart_details: String| -> 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" );}