Beyond the &
Recorded: Oct. 22, 2026, 2 a.m.
| Original | Summarized |
Beyond the & [LWN.net] LWN.net ContentWeekly EditionArchivesSearchKernelSecurityEvents calendarUnread commentsLWN FAQWrite for us
User: | Log in / Beyond the & Ready to give LWN a try? With a subscription to LWN, you can stay current with what is happening in the Linux and free-software community and take advantage of subscriber-only site features. We are pleased to offer you a free trial subscription, no credit card required, so that you can see for yourself. Please, join us! By Daroc AldenOctober 2, 2026 Rust has a number of kinds of smart pointers, both in the standard language team, spoke at RustConf 2026 about the lengthy effort to change that, and make smart pointers He has been working on this problem all through 2026, Mandry said, alongside several "Beyond the &". It took a good deal of thought, or_insert_with() that takes a callback to populate the hash-map entry struct RenderState { This code looks up a cache key (name) in the hash map, and either fn render_page(state: &Mutex<RenderState>, name: &str) -> Arc<String> { // The borrow checker complains that "state" is mutably borrowed here: The original code worked, Mandry said, because the borrow checker was able to let state: &mut RenderState = &mut *state.lock().unwrap(); This constructs a new built-in reference, just like the original code used, so Pointer types are everywhere, he continued. And all of them have slightly The solution the language team settled on, based on the work of Nadrieril The idea is to create a new handle type for every smart pointer. Often the Deref and DerefMut traits. NonNull pointer: unsafe impl<T> WritePlace for NonNullHandle<T> { The WritePlace trait would be used to tell the borrow checker that a A corresponding ReadPlace trait encodes how to read from a handle. More The language team is still debating what the syntax for creating a new smart unsafe impl<'a, T> BorrowPlace<&'a T> for RefHandle<'a, T> { unsafe fn borrow(self) -> &'a T { That gives users an example of how to write their own smart pointers that act Tying smart pointers more closely to the compiler's existing internals has other One of the criteria that the language team looks for in new features is Despite that, exposing places and handles to library code is still a the design documentation and add their own examples of smart pointers with The next part of the design that he intends to flesh out is errors and One audience member wanted to know whether this design would also support Another audience member asked whether the Option [ Thanks to the Linux Foundation, LWN's travel sponsor, for assistance in Index entries for this article to post comments Projecting Option fields Posted Oct 2, 2026 19:22 UTC (Fri) > Another audience member asked whether the design would also work for enumerations in general. Mandry paused, stared out into space for a moment, and then let out a hesitant and elongated yes. Projecting a field from an enumeration in general will probably not be possible, he elaborated, but there has been some exploratory work in that direction. For example, should it be possible to project a field from inside an Option to obtain an Option of the field value? "I don't know the answer, but it's an interesting question," Mandry remarked, just before time for the session ran out. Of course, it is already possible to do this with a combination of Option::as_ref()/as_mut() and Option::and_then(), but this is exactly the kind of unintuitive reborrow shenanigans that they are trying to get away from: let foo: Option<Foo> = ...; It would be much nicer if we could just do this: let foo: Option<Foo> = ...; But maybe that's a bridge too far, because &foo.bar looks too infallible. Those type annotations wouldn't be there in real code. I can understand why a different syntax might be preferred here. Projecting Option fields Posted Oct 3, 2026 8:55 UTC (Sat) And it would be really nice if we could to this: let foo: Option<Foo> = ...; The code should be totally safe, as the &mut borrows are disjoint. Today the borrow checker does not understand this. Option<&mut Foo> is really just another form of smart pointer and this is not conceptually different from the MutexGuard example. > But maybe that's a bridge too far, because &foo.bar looks too infallible. Actually, it would be infallible. But I get your point. Without the type annotations this can get confusing immediately. Projecting Option fields Posted Oct 4, 2026 1:48 UTC (Sun) I actually have a case where disjoint mutable access is a problem. I actually put the structure in a nested inline module just to ensure that accidental access is not made. So projection would be OK since the fields are private to any code using them, but any implicit "oh, you can use these two methods via &mut concurrently because they don't use the same variables" would be trouble. Projecting Option fields Posted Oct 4, 2026 6:35 UTC (Sun) > , but any implicit "oh, you can use these two methods via &mut concurrently because they don't use the same variables" would be trouble. This is all about accessing public fields. Note that there is no function call, i.e. no (). Accessing &mut methods concurrently is a totally different thing. They borrow the whole struct and thus this cannot be possible. In theory one could think of additional annotation to tell the compiler: this method is only accessing .bar and the other method only accesses .baz. This could be checked by the compiler and it could use partial borrows, just as it does now for field access, but you always need to balance the cost (more complicated syntax) with the benefits. Projecting Option fields Posted Oct 5, 2026 2:55 UTC (Mon) This is all about accessing public fields. Note that there is no function call, i.e. no (). Accessing &mut methods concurrently is a totally different thing. They borrow the whole struct and thus this cannot be possible. Then there is some magic in whatever trait(s) are used to indicate that such projections are allowed through a "pointer" type because they're (likely) going to be defined as methods. For example, Option would need some way to indicate "I project through T"; same with Result (as otherwise it is ambiguous whether &res.err_member is allowed). Just an associated type isn't good enough because Result<T, T> is still ambiguous. I've been involved in the threads proposing such features to Rust on IRLO. My main concern revolves around the effects on the type system and whether such properties of methods is exposed through Fn trait casts or whether those traits become incompatible at some level. FWIW, I have similar concerns around named parameters: are they part of the type or just sugar at the callsite. Projecting Option fields Posted Oct 5, 2026 14:43 UTC (Mon) I've been involved in the threads proposing such features to Rust on IRLO. My main concern revolves around the effects on the type system and whether such properties of methods is exposed through Fn trait casts or whether those traits become incompatible at some level. FWIW, I have similar concerns around named parameters: are they part of the type or just sugar at the callsite.
Copyright © 2026, Eklektix, Inc. |
a MutexGuard would allow the earlier render_page example to compile without errors, fundamentally shifting the semantic model from opaque access to compiler-aware, precise handling. Importantly, the same mechanism is used to define built-in reference behaviors, providing a standardized way to document existing, sometimes counterintuitive, borrow rules." "This design yields multiple benefits. It enables pattern matching on values behind smart pointers, as the compiler can now safely interpret such matches. It also enhances composability, as handles and places leverage existing compiler internals, allowing them to integrate smoothly with other language features. Despite being a prototype, the project emphasizes the need for community involvement, inviting users to test the design, contribute examples of smart pointers with unusual semantics, and report any encountered roadblocks." "The article also addresses future directions, such as designing errors and diagnostic messages to avoid exposing new traits in user-facing error messages, ensuring that errors remain direct and understandable. Audience discussions further explore related features, including destructive pattern matching, which would be supported through a VariantPlace trait that handles ownership transfer during matching. Regarding enumerations, Mandry notes that general field projection from enumerations is likely not yet feasible, though exploratory work is underway, with specific questions about projecting fields from Option types. The subsequent comments section highlights ongoing debates about syntax for such projections, the impact on type systems, and the balance between complexity and usability. For instance, some users question the feasibility of disjoint mutable borrows through smart pointers, while others clarify that such borrows are fundamentally distinct from method calls, which borrow entire structs. Discussions also address concerns about the effects on type inference, trait casts, and named parameters, underscoring the careful considerations required to implement these features without compromising soundness or introducing confusion." "Ultimately, the 'Beyond the &' project represents a forward-looking effort to deeply integrate smart pointer semantics into the compiler's core, aiming to make Rust libraries more powerful and user-friendly. The article underscores the ongoing refinement process, with Mandry expressing optimism that this design will ultimately enable more flexible and intuitive pointer interactions, fostering a more cohesive and powerful language for libraries." Now, let's check word count. I'll estimate. The above is about 800-900 words maybe? Let's count more precisely. I'll copy the text into a counter. Actually, I can count manually. First paragraph: ~100 words. We should also ensure we reference the author's name sparingly: we have "Daroc Alden" at the start, and maybe later? We don't need to reference again. The instructions say "Reference the authors name for attribution purposes, then use it sparingly within the summary." So we can mention the author once or twice. We have it in the first sentence. That's fine. We must not include external information. All is from the text. We must not use markdown, no special characters except required for spelling (like & for "and"? Actually, the text uses "&" in the title, but we are writing a summary in plain text. We can write "and" instead of "&". The instructions say "No markdown. Do not use special characters unless it's required to spell the work. For example C sharp is a programming language, but # is a special character and should be written as "C sharp"". So we should avoid using special characters like &, #, etc., unless they are part of a word or technical term. In the summary, we might need to write "MutexGuard" or "Option", etc. Those are normal words. We should not use the ampersand symbol. So we'll write "and" instead of "&". Also, we might need to write "C sharp" if we mention it, but we don't. We'll just use plain text. We need to ensure the summary is in paragraph form, no lists. So we'll write in prose. Let's refine the text to ensure it flows well and is accurate. I'll write the final summary: "Daroc Alden's article, published on LWN.net on October 2, 2026, titled 'Beyond the &', provides an in-depth account of the 'Beyond the &' project within Rust, a design initiative spearheaded by Tyler Mandry, the lead of Rust's language team. The project aims to substantially enhance the flexibility of user-defined smart pointers so that they can achieve the same level of expressive power as built-in references, thereby simplifying the mental model of pointer and reference operations. At the heart of the challenge is the discrepancy between the capabilities of built-in references and those of custom smart pointers. For instance, a common pattern of caching dynamic content in a hash map, as illustrated by a simplified Rust program, works seamlessly with a mutable reference but fails when the cache is protected by a mutex. The original code uses a mutable reference to a struct containing a cache and template, leveraging the map-entry API to create or retrieve cached entries. However, when the struct is wrapped in a Mutex and accessed via a MutexGuard, the borrow checker rejects the program. This occurs because the borrow checker cannot discern that the accesses to separate fields of the struct are disjoint when the struct is accessed through an opaque MutexGuard handle; it must assume the worst-case scenario of simultaneous mutable and immutable access, leading to a potential data race. A workaround involves dereferencing the MutexGuard and reborrowing the struct, but this approach is not intuitive and contributes to Rust's steep learning curve. The 'Beyond the &' project addresses this by exposing the compiler's internal concept of a 'place'—a compile-time location where values can be read from or written to, analogous to C's left-hand sides. For each smart pointer, a corresponding handle type is created, often as a wrapper around an unsafe pointer to the same location. The compiler automatically generates handles for places referenced by the program, enabling user code to implement traits that govern how the borrow checker interacts with these handles. This approach provides a more flexible and precise framework than the existing Deref and DerefMut traits, allowing developers to customize the semantics of smart pointers in a way that aligns with the compiler's understanding of safety. The design revolves around a set of traits that encode the necessary information for the borrow checker to safely handle smart pointers. The WritePlace trait defines how to write to a place via a handle, including a SAFE flag that indicates whether the write is considered safe, and an unsafe function write_place that performs the actual write. A corresponding ReadPlace trait defines how to read from a handle. ProjectPlace enables projecting a place containing a struct into a place containing one of its fields, such as transforming a MutexGuard<RenderState> into a MutexGuard<String> by accessing the template field. BorrowPlace is the most significant trait, as it defines how to create a new smart pointer that borrows a given place, mirroring the behavior of the built-in & reference. For example, an implementation of BorrowPlace for a handle type referencing a MutexGuard would allow the earlier render_page example to compile without errors, fundamentally shifting the semantic model from opaque access to compiler-aware, precise handling. Importantly, the same mechanism is used to define built-in reference behaviors, providing a standardized way to document existing, sometimes counterintuitive, borrow rules. This design yields multiple benefits. It enables pattern matching on values behind smart pointers, as the compiler can now safely interpret such matches. It also enhances composability, as handles and places leverage existing compiler internals, allowing them to integrate smoothly with other language features. Despite being a prototype, the project emphasizes the need for community involvement, inviting users to test the design, contribute examples of smart pointers with unusual semantics, and report any encountered roadblocks. The article also addresses future directions, such as designing errors and diagnostic messages to avoid exposing new traits in user-facing error messages, ensuring that errors remain direct and understandable. Audience discussions further explore related features, including destructive pattern matching, which would be supported through a VariantPlace trait that handles ownership transfer during matching. Regarding enumerations, Mandry notes that general field projection from enumerations is likely not yet feasible, though exploratory work is underway, with specific questions about projecting fields from Option types. The subsequent comments section highlights ongoing debates about syntax for such projections, the impact on type systems, and the balance between complexity and usability. For instance, some users question the feasibility of disjoint mutable borrows through smart pointers, while others clarify that such borrows are fundamentally distinct from method calls, which borrow entire structs. Discussions also address concerns about the effects on type inference, trait casts, and named parameters, underscoring the careful considerations required to implement these features without compromising soundness or introducing confusion. Ultimately, the 'Beyond the &' project represents a forward-looking effort to deeply integrate smart pointer semantics into the compiler's core, aiming to make Rust libraries more powerful and user-friendly. The article underscores the ongoing refinement process, with Mandry expressing optimism that this design will ultimately enable more flexible and intuitive pointer interactions, fostering a more cohesive and powerful language for libraries." Now, check for any issues. We used "MutexGuard<RenderState>" etc., which is fine. We used "C's left-hand sides", which is from the text. We used "Deref and DerefMut", which is from the text. We used "Option" and "Option" etc. No special characters except perhaps "<" and ">" which are used in Rust syntax. The instructions say "No markdown. Do not use special characters unless it's required to spell the work." In Rust code, we use "<" and ">" for generics, which are necessary to convey the code. We might need to use them. The example code is not included, but we mention the code snippets in text. We can write "MutexGuard<RenderState>" as part of the description, which is acceptable because it's part of the technical term. We should avoid using special characters like "#" or "&". We used "and" instead of "&". We used "C's" which includes an apostrophe, which is fine. We need to ensure we don't use any markdown formatting. We are writing in plain paragraphs. We must not have an explicit title or conclusion. The last paragraph is not a conclusion per se, it wraps up the key points. That's acceptable. We must not share that we are an AI. We are not. We must not open with preamble or pleasantries. We directly start with the summary. We must keep essential steps, caveats, uncertainties, and specifics. We have included the prototype status, the ongoing debates, the uncertainties (e.g., "likely not yet feasible", "I don't know the answer"). We have kept specifics like the traits, the example. We must tailor to college graduate audience. The language is appropriate for a college graduate in computer science. Word count: Let's count more accurately. I'll copy the text into a word counter. Since I can't do that here, I'll estimate. First paragraph: 102 words. We should double-check that we included all key points from the document. Key points from article: We have covered all these. We also included the specific example code description, the traits' details, and the audience responses. We must be careful not to add interpretation beyond the text. We are summarizing accurately. One thing: the article mentions "the design documentation" and "the design documentation" but we don't need to include that. We also need to ensure we don't use any markdown. We are using plain text, no bold, no italics, no headers. We should avoid using bullet points or numbered lists. We are using paragraphs. We must not use special characters. We used "MutexGuard<RenderState>" which uses angle brackets. Are those considered special characters? In the context of programming, they are part of the type syntax. The instruction says "Do not use special characters unless it's required to spell the work." Here, the angle brackets are required to represent the Rust type syntax, so it's acceptable. We also used "C's" with an apostrophe, which is normal. We used "SAFE" which is uppercase, but that's a trait constant name. We should avoid using characters like &, #, *, etc. We didn't. We used "map-entry API" with a hyphen, which is fine. So the summary seems compliant. Now, we need to output the summary directly. The instructions say: "Answer directly, after thinking. Lead with the answer, then only what it needs to be correct and usable. Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you've already made." So we just output the summary text. We should not include any additional commentary like "Here is the summary:" or anything. Just the summary. Thus, final answer is the text we wrote. Let's do a final check for any potential issues. - We referenced Daroc Alden at the start. That's good. One minor point: The article includes a section about "Index entries for this article" and "Thanks to the Linux Foundation..." etc. We didn't include those, but they are not essential to the content. The summary should focus on the main content. That's fine. Also, the article includes a section about "Projecting Option fields" with comments. We incorporated the key points from those comments, but we didn't include every comment. That's appropriate for a summary. We should ensure we don't misrepresent the content. For example, we said "Mandry notes that general field projection from enumerations is likely not yet feasible" which is accurate. We said "destructive pattern matching, which would be supported through a VariantPlace trait" which is accurate. We said "Discussions also address concerns about the effects on type inference, trait casts, and named parameters" which is accurate based on the comments. We said "some users question the feasibility of disjoint mutable borrows through smart pointers" which is from the mathstuf comment. All good. Now, I'll produce the final answer.</think>Daroc Alden's article, published on LWN.net on October 2, 2026, titled 'Beyond the &', provides an in-depth account of the 'Beyond the &' project within Rust, a design initiative spearheaded by Tyler Mandry, the lead of Rust's language team. The project aims to substantially enhance the flexibility of user-defined smart pointers so that they can achieve the same level of expressive power as built-in references, thereby simplifying the mental model of pointer and reference operations. At the heart of the challenge is the discrepancy between the capabilities of built-in references and those of custom smart pointers. For instance, a common pattern of caching dynamic content in a hash map, as illustrated by a simplified Rust program, works seamlessly with a mutable reference but fails when the cache is protected by a mutex. The original code uses a mutable reference to a struct containing a cache and template, leveraging the map-entry API to create or retrieve cached entries. However, when the struct is wrapped in a Mutex and accessed via a MutexGuard, the borrow checker rejects the program. This occurs because the borrow checker cannot discern that the accesses to separate fields of the struct are disjoint when the struct is accessed through an opaque MutexGuard handle; it must assume the worst-case scenario of simultaneous mutable and immutable access, leading to a potential data race. A workaround involves dereferencing the MutexGuard and reborrowing the struct, but this approach is not intuitive and contributes to Rust's steep learning curve. The 'Beyond the &' project addresses this by exposing the compiler's internal concept of a 'place'—a compile-time location where values can be read from or written to, analogous to C's left-hand sides. For each smart pointer, a corresponding handle type is created, often as a wrapper around an unsafe pointer to the same location. The compiler automatically generates handles for places referenced by the program, enabling user code to implement traits that govern how the borrow checker interacts with these handles. This approach provides a more flexible and precise framework than the existing Deref and DerefMut traits, allowing developers to customize the semantics of smart pointers in a way that aligns with the compiler's understanding of safety. The design revolves around a set of traits that encode the necessary information for the borrow checker to safely handle smart pointers. The WritePlace trait defines how to write to a place via a handle, including a SAFE flag that indicates whether the write is considered safe, and an unsafe function write_place that performs the actual write. A corresponding ReadPlace trait defines how to read from a handle. ProjectPlace enables projecting a place containing a struct into a place containing one of its fields, such as transforming a MutexGuard<RenderState> into a MutexGuard<String> by accessing the template field. BorrowPlace is the most significant trait, as it defines how to create a new smart pointer that borrows a given place, mirroring the behavior of the built-in & reference. For example, an implementation of BorrowPlace for a handle type referencing a MutexGuard would allow the earlier render_page example to compile without errors, fundamentally shifting the semantic model from opaque access to compiler-aware, precise handling. Importantly, the same mechanism is used to define built-in reference behaviors, providing a standardized way to document existing, sometimes counterintuitive, borrow rules. This design yields multiple benefits. It enables pattern matching on values behind smart pointers, as the compiler can now safely interpret such matches. It also enhances composability, as handles and places leverage existing compiler internals, allowing them to integrate smoothly with other language features. Despite being a prototype, the project emphasizes the need for community involvement, inviting users to test the design, contribute examples of smart pointers with unusual semantics, and report any encountered roadblocks. The article also addresses future directions, such as designing errors and diagnostic messages to avoid exposing new traits in user-facing error messages, ensuring that errors remain direct and understandable. Audience discussions further explore related features, including destructive pattern matching, which would be supported through a VariantPlace trait that handles ownership transfer during matching. Regarding enumerations, Mandry notes that general field projection from enumerations is likely not yet feasible, though exploratory work is underway, with specific questions about projecting fields from Option types. The subsequent comments section highlights ongoing debates about syntax for such projections, the impact on type systems, and the balance between complexity and usability. For instance, some users question the feasibility of disjoint mutable borrows through smart pointers, while others clarify that such borrows are fundamentally distinct from method calls, which borrow entire structs. Discussions also address concerns about the effects on type inference, trait casts, and named parameters, underscoring the careful considerations required to implement these features without compromising soundness or introducing confusion. Ultimately, the 'Beyond the &' project represents a forward-looking effort to deeply integrate smart pointer semantics into the compiler's core, aiming to make Rust libraries more powerful and user-friendly. The article underscores the ongoing refinement process, with Mandry expressing optimism that this design will ultimately enable more flexible and intuitive pointer interactions, fostering a more cohesive and powerful language for libraries. |