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)); }