Closures

ํด๋กœ์ € ํ˜น์€ ๋žŒ๋‹คํ‘œํ˜„์‹์€ ์ต๋ช…ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ์ด๋“ค์€ Fn, FnMut, FnOnce ๋ผ๋Š” ํŠน๋ณ„ํ•œ ํŠธ๋ ˆ์ดํŠธ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Closures or lambda expressions have types which cannot be named. However, they implement special Fn, FnMut, and FnOnce traits:

fn apply_with_log(func: impl FnOnce(i32) -> i32, input: i32) -> i32 {
    println!("Calling function on {input}");
    func(input)
}

fn main() {
    let add_3 = |x| x + 3;
    let mul_5 = |x| x * 5;

    println!("add_3: {}", apply_with_log(add_3, 10));
    println!("mul_5: {}", apply_with_log(mul_5, 20));
}