gpt4 book ai didi

rust - 为什么 Fn 派生自 FnMut(派生自 FnOnce)?

转载 作者:行者123 更新时间:2023-11-29 07:51:00 29 4
gpt4 key购买 nike

如果您查看 official Rust doc ,您会看到特征 Fn 派生自 FnMut,或者,要实现 Fn,您必须实现 FnMut(之后是 FnOnce,因为 FnMut 也派生自它)。

为什么会这样?我简直无法理解。是因为您可以将每个 Fn 调用为 FnOnceFnMut 吗?

最佳答案

对此最好的引用是优秀的 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 and FnOnce,
  • FnMut and FnOnce,
  • only FnOnce.

Why? Well, the three closure traits are actually three nested sets: every closure that implements Fn can also implement FnMut (if &self works, &mut self also works; proof: &*self), and similarly every closure implementing FnMut can also implement FnOnce. This hierarchy is enforced at the type level

关于rust - 为什么 Fn 派生自 FnMut(派生自 FnOnce)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31190851/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com