Function Pointers

Exercises

As parameters

// Complete the function signature for `factorial`. It must not contain any generics/traits.

fn decrement(x: u32) -> u32 {
    x - 1
}

fn multiply(x: u32, y: u32) -> u32 {
    x * y
}

fn factorial(num, dec, mul) {
    let mut res = 1;
    let mut temp = num;
    while temp > 1 {
        res = mul(res, temp);
        temp = dec(temp);
    }
    res
}

fn main() {
    let num = 6;
    let fact = factorial(num, decrement, multiply);
    println!("{num}! = {fact}");
}
Solution
fn decrement(x: u32) -> u32 {
  x - 1
}

fn multiply(x: u32, y: u32) -> u32 {
  x * y
}

fn factorial(num: u32, dec: fn(u32)->u32, mul: fn(u32, u32)->u32) -> u32 {
  let mut res = 1;
  let mut temp = num;
  while temp > 1 {
      res = mul(res, temp);
      temp = dec(temp);
  }
  res
}

fn main() {
  let num = 6;
  let fact = factorial(num, decrement, multiply);
  println!("{num}! = {fact}");
}

Coercing from closures

// Fix the code by addressing the TODO.

fn invoker<T>(logic: fn(T), arg: T) {
    logic(arg);
}

fn main() {
    // TODO: shift below declaration to somewhere else
    let greeting = String::from("Nice to meet you");
    let greet = |name| {
        println!("{greeting} {name}!");
    };
    invoker(greet, "Jenny");
}
Solution
fn invoker<T>(logic: fn(T), arg: T) {
  logic(arg);
}

fn main() {
  let greet = |name| {
      let greeting = String::from("Nice to meet you");
      println!("{greeting} {name}!");
  };
  invoker(greet, "Jenny");
}