// Make the code compile by addressing the TODO.
fn main() {
let my_str = "Hi there!".to_owned();
let substr = "here";
let check_substr = move |sub: &str| my_str.contains(sub);
let res = check_substr(substr);
// TODO: shift the statement below to somewhere else
println!("String: {my_str}");
println!("String contains {substr} : {res}");
}
Solution
fn main() {
let my_str = "Hi there!".to_owned();
let substr = "here";
println!("String: {my_str}");
let check_substr = move |sub: &str| my_str.contains(sub);
let res = check_substr(substr);
println!("String contains {substr} : {res}");
}
// Complete the function signature.
fn average<T, U>(nums: &[i32], add: T, div: U) -> f32
where
{
let mut sum = 0;
for num in nums {
sum = add(sum, *num);
}
div(sum, nums.len() as i32)
}
fn main() {
let add = |a, b| a + b;
let div = |a, b| a as f32 / b as f32;
let my_nums = [1, 2, 3, 4, 5];
let res = average(&my_nums, add, div);
println!("Average of {my_nums:?} = {res}");
}
Solution
fn average<T, U>(nums: &[i32], add: T, div: U) -> f32
where T: Fn(i32, i32) -> i32,
U: Fn(i32, i32) -> f32,
{
let mut sum = 0;
for num in nums {
sum = add(sum, *num);
}
div(sum, nums.len() as i32)
}
fn main() {
let add = |a, b| a + b;
let div = |a, b| a as f32 / b as f32;
let my_nums = [1, 2, 3, 4, 5];
let res = average(&my_nums, add, div);
println!("Average of {my_nums:?} = {res}");
}