如果您查看 official Rust doc ,您会看到特征 Fn
派生自 FnMut
,或者,要实现 Fn
,您必须实现 FnMut
(之后是 FnOnce
,因为 FnMut
也派生自它)。
为什么会这样?我简直无法理解。是因为您可以将每个 Fn
调用为 FnOnce
或 FnMut
吗?
最佳答案
对此最好的引用是优秀的 Finding Closure in Rust博客文章。我将引用重要部分:
There’s three traits, and so seven non-empty sets of traits that could possibly be implemented… but there’s actually only three interesting configurations:
Fn
,FnMut
andFnOnce
,FnMut
andFnOnce
,- only
FnOnce
.Why? Well, the three closure traits are actually three nested sets: every closure that implements
Fn
can also implementFnMut
(if&self works
,&mut self
also works; proof:&*self
), and similarly every closure implementingFnMut
can also implementFnOnce
. This hierarchy is enforced at the type level
关于rust - 为什么 Fn 派生自 FnMut(派生自 FnOnce)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190851/