The problem of effects in Rust

2020年4月13日

AI 摘要 AI 生成,可能有误

The author argues that several seemingly separate discussions in the Rust community—such as writing fallible code inside iterator adapters and closures that maintain Tennant's Correspondence Principle—are all deeply interconnected. In his view, they all stem from what he considers one of the biggest design problems in Rust: the language's failure at version 1.0 to provide good support for handling common effects related to program control flow.

In a previous post, I shortly discussed the concept of “effects” and the parallels between them. In an unrelated post since then, Yosh Wuyts writes about the problem of trying to write fallible code inside of an iterator adapter that doesn’t support it. In a previous discussion, the users of the Rust Internals forum hotly discuss the notion of closures which would maintain the so-called “Tennant’s Correspondence Principle” - that is, closures which support breaking to scopes outside of the closure, inside of the function they are in (you can think of this is closures capturing their control flow environment in addition to capturing variables).

I think it may not be obvious, but these discussions are all deeply related. They all arise from what is, in my opinion, one of the biggest problems with the design of the Rust language: its failure at 1.0 to give good support for handling common effects related to program control flow.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Without Boats

Generators with UnpinCell

AI 摘要The author argues that the newly proposed UnpinCell wrapper type, which converts a pinned mutable reference into a plain mutable reference, could solve the biggest outstanding problem with Rust generators: the Iterator interface does not permit self-referential values. Because Pin was designed as a backward-compatible, opt-in contract, pre-existing traits like Iterator—which takes a plain mutable reference rather than a pinned one—cannot be implemented by self-referential types. As a result, generators cannot support self-referential values the way async functions do, unless the Iterator interface adopts pinned references.

UnpinCell

AI 摘要The author proposes a new Rust design, UnpinCell, as a variation on their earlier pinned places proposal. Whereas the previous design relied on the unusual concept of 'pinned fields' to support pinned projection, this new approach aims to offer similar properties while remaining consistent with Rust's standard field projection behavior, without introducing a special field marker.

Pinned places

AI 摘要The article explains that Rust's Pin type was designed in 2018 as a minimal, library-only solution to enable shipping async/await quickly, but this choice created a significant complexity cliff for users who must interact with pinned references, especially when implementing Future by hand. The author argues that these difficulties stem not from the inherent complexity of pinning as a concept, but from Pin being a pure library type lacking language support. The post proposes a set of fully backward-compatible language changes to make working with pinned references feel much closer to working with ordinary references.

Pin

AI 摘要The article discusses Pin, a foundational but widely misunderstood part of Rust's async ecosystem that supports self-referential types by preventing them from moving in memory. The author, who originally proposed adding Pin to Rust, acknowledges critiques (such as one from the makers of Mojo) that interacting with Pin creates a 'complexity cliff' for users, and aims to explain what Pin achieves, how it came to be, and why it is hard to use. The author promises to present ideas for resolving this complexity in a follow-up post.

Ownership

AI 摘要This article explains how substructural type theory applies to programming language design, arguing that designers should use accessible metaphors like 'ownership' to help ordinary programmers understand concepts such as constructors, destructors, and preventing aliasing of objects that represent external resources. Using Rust's ownership model as the primary example, the author aims to demystify these ideas so they become more common knowledge among programmers. While the post contains nothing groundbreaking for experts, it includes notes critiquing certain aspects of Rust's implementation that the author believes are incorrect.