Lobsters | 📄 原文链接 | 2026-07-22 collected

捕获子句作为效果:Rust 闭包语法的全新设计

source: blog.yoshuawuyts.com — 2026-07-15

概述

Yosh Wuyts 分析了 RFC 3968 中 move($expr) 提案的问题,提出将捕获行为建模为语言级效果(effect)的方案。文章系统对比了 10 种常见闭包捕获模式(全 move、全 clone、混合 move/clone/引用、结构体字段级捕获等),论证显式捕获子句 + 效果系统比 move($expr) 的逆 defer 更简洁、更一致。核心目标:改善 Bevy 等 clone 密集型生态系统的 Rust 开发体验。

核心要点

  • 对比 10 种闭包捕获模式:全 move、全 clone、部分 clone、字段级捕获、引用与 move 混合等。
  • RFC 3968 的 move($expr) 方案类似 逆 defer:在闭包实例化时而非作用域结束时执行。
  • 作者提议:将 捕获行为建模为效果——可以按闭包变量粒度指定 move/clone/ref 语义。
  • 主要受益场景:Bevy 等 clone 密集型生态系统,当前每次闭包都需手动 clone 三五行。

金句

I feel that explicit capture clauses provide a simpler model and are therefore preferable. But they have the downside they can feel a bit clunky to write.
back to Lobsters