yohhoyの日記

技術的メモをしていきたい日記

ラムダ式はFnOnce/FnMut/Fnトレイト実装の糖衣構文

プログラミング言語Rustのラムダ式(lambda expression)は、トレイトFnOnce / FnMut / Fn を実装(impl)したクロージャ(closure)を作り出す、一種の糖衣構文(syntax sugar)として機能する。

Closure implementation
Rust's implementation of closures is a bit different than other languages. They are effectively syntax sugar for traits. You'll want to make sure to have read the traits chapter before this one, as well as the chapter on trait objects.

(snip)

The || {} syntax for closures is sugar for these three traits. Rust will generate a struct for the environment, impl the appropriate trait, and then use it.

https://doc.rust-lang.org/book/closures.html

関連URL