Daily Tech Briefing
AI 科技速览

每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。

AI 快讯
Hacker News · 2026/8/3 06:42:00

Rust project goals: Immobile types and guaranteed destructors

AI 中文解读
核心亮点:Rust编程语言正在酝酿一场“底层能力革命”,让类型能主动声明“我可以被移动”或“我必须被清理”,解决异步编程中最棘手的自引用和资源释放难题。 通俗解读:想象你有一份重要合同,既怕被人随手挪走弄乱顺序,又怕被草率扔掉没走完审批流程。过去所有数据都默认允许搬动和遗忘,导致开发者要额外加“安全带”。Rust这次要让数据类型自己说“不”:能声明“别动我”或“必须销毁我”。这意味着写异步任务和系统底层代码时,不再需要复杂的绕路操作,编译器能自动保证安全。 实际影响:对普通用户来说,这项改进藏在后台,但会带来更稳定的软件和更流畅的异步应用体验,比如服务器能更高效地处理海量连接。对程序员来说,尤其是Linux内核和嵌入式开发者,写并发代码会变得更简单、更不容易出错,降低内存泄漏和悬空引用的风险。长期看,这能推动Rust在更多关键系统中落地,让日常使用的软件更可靠。
Immobile types and guaranteed destructors Metadata Point of contact @lcnr Status Accepted What and why Let types opt out of being moved or forgotten, enabling scoped spawn, async drop, and pin-by-default Timespan 2026-2027 Roadmap Just add async Roadmap Rust for Linux Tracking issue [#635] Other tracking issues [rust-lang/rust#149607] Zulip channel #t-lang/move-trait [types] champion @lcnr [lang] champion @jackh726 Summary We propose to introduce new traits that describe what operations are possible on a type. Today Rust assumes all types can be moved (relocated in memory) and forgotten (via mem::forget). We will introduce traits like Move and Forget that make these capabilities explicit, allowing types to opt out. This follows the precedent set by the Sized hierarchy work, which relaxes the assumption that all types have a compile-time-known size. We will implement MVPs in the compiler, write RFCs, and validate viability through real-world testing in the Linux Kernel. Motivation The status quo Rust has historically assumed that all values can be moved (relocated in memory) and forgotten (via mem::forget, without running destructors). These assumptions are baked into the language: assignment moves values, and mem::forget is safe. But some types need to opt out of these capabilities: Immobile types: A lot of async futures want to be self-referential, but self-referential types can't be safely moved. The current solution is Pin, which encodes immovability as a property of places rather than types. This leads to significant complexity. As The Safe Pinned Initialization Problem describes, Pin struggles to safely encode self-referential types in systems like the Linux kernel. Guaranteed destructors: Some types need their destructors to run. A Transaction type might require commit() or rollback() before cleanup. A scoped task handle must join before the scope exits. But mem::forget is safe, so Rust can't guarantee destructors run. This blocks patterns like safe scoped spawn for async, where the spawned task borrows from the parent scope. What we propose to do about it We propose to generalize Rust's type system with new auto-traits that describe what operations are possible on a type. The framing is positive: traits represent capabilities. At the base layer, types may have no special capabilities. We then layer on the things we need: Move: The type can be relocated in memory. Destruct: The type can be implicitly dropped (destructor runs when it goes out of scope). Forget: The type can be forgotten via mem::forget without running its destructor. This follows the precedent set by the Sized hierarchy work. Just as that work relaxes "all types have compile-time-known size" to support scalable vectors, this work relaxes "all types can be moved" and "all types can be forgotten." The Move trait encodes movability as a property of types rather than places: #[lang = "move"] unsafe auto trait Move {} Types implementing !Move cannot be moved and must keep a stable address for their entire existence. This is simpler than Pin because immovability is a type property, not a place property. Construction of !Move types will rely on work from #t-lang/in-place-init. The Forget trait lets types opt out of being forgettable: // Types implementing !Forget must have their destructors run unsafe impl !Forget for ScopedTaskHandle {} With !Forget, we could build safe scoped spawn: the handle's destructor joins the task, and because the handle can't be forgotten, the join is guaranteed. This unblocks patterns that are currently impossible in safe Rust. Work items over the next year Move trait Let types opt out of being relocated in memory, encoding immovability as a type property rather than a place property. Task Owner(s) Notes Compiler implementation for Move @lcnr and @nia-e Write the Move RFC @yoshuawuyts Test in Linux kernel @BennoLossin RfL is an important Rust user which uses a lot of self-referential data
分享
阅读原文