|
|
Log in / Subscribe / Register

Beyond the &

By Daroc Alden
October 2, 2026

RustConf

Rust has a number of kinds of smart pointers, both in the standard library and defined by users. Still, some operations that are possible with built-in references are not possible to perform with user-defined smart pointers. Tyler Mandry, lead of the Rust project's language team, spoke at RustConf 2026 about the lengthy effort to change that, and make smart pointers just as flexible as built-in references.

He has been working on this problem all through 2026, Mandry said, alongside several other interested members of the language team, under the project name "Beyond the &". It took a good deal of thought, but the design that they have arrived at should allow a substantially simpler mental model of how pointers and references work. To illustrate the problem that this design solves, he put up an example of a simplified Rust program that calculates some dynamic content and caches it in a hash map. The example uses Rust's map-entry API, which provides a function called or_insert_with() that takes a callback to populate the hash-map entry if it is empty.

    struct RenderState {
        cache: HashMap<String, Arc<String>>,
        template: String,
    }
    
    fn render_page(state: &mut RenderState, name: &str) -> Arc<String> {
        // Mutably borrow `state.cache` using the Entry API.
        let entry = state.cache.entry(name.to_string());
        entry.or_insert_with(|| {
            // Create the entry if it doesn't exist.
            Arc::new(state.template.replace("$name", name))
        }).clone()
    }

This code looks up a cache key (name) in the hash map, and either returns a copy of the cached value, or calculates it by substituting name into a template if it doesn't exist. The shared state is accessed using a built-in mutable reference, &mut RenderState. The example works, but it can't safely be shared between threads. Adding a mutex, to allow the cache to be shared, causes the borrow checker to reject the program:

    fn render_page(state: &Mutex<RenderState>, name: &str) -> Arc<String> {
        let state: MutexGuard <' _ , RenderState> = state.lock().unwrap();

        // The borrow checker complains that "state" is mutably borrowed here:
        let entry = state.cache.entry(name.to_string());
        entry.or_insert_with(|| {
            // ... but then used here while it is still borrowed:
            Arc::new(state.template.replace("$name", name))
        }).clone()
    }

The original code worked, Mandry said, because the borrow checker was able to track that state.cache and state.template were separate fields, so it was safe to access them at the same time. With the addition of the mutex, the borrow checker just sees opaque accesses to a MutexGuard structure, and can no longer tell that the accesses are disjoint — it has to consider the case where the mutable borrow is used to edit the state at the same time that the callback reads from it, causing a potentially invalid data race. A simple fix in this case is to dereference the MutexGuard once, and reborrow the structure behind it:

        let state: &mut RenderState = &mut *state.lock().unwrap();

This constructs a new built-in reference, just like the original code used, so the borrow checker can once again see that the accesses don't interfere with each other. That works, but it's not particularly intuitive. Mandry said that complexity like this contributes to Rust's difficult learning curve. It would be better if user-defined pointer types such as MutexGuard behaved more like built-in references.

[Tyler Mandry]

Pointer types are everywhere, he continued. And all of them have slightly different semantics. Some pointers can't be safely dereferenced (raw pointers), or can be written to but not read from (MaybeUninit), etc. This demonstrates Rust's versatility, but it also makes it hard to come up with a design that works for the many possible pointer types.

The solution the language team settled on, based on the work of Nadrieril and Benno Lossin, was to expose the compiler's internal notion of a "place" to user code. A place is Rust's equivalent of C's left-hand sides (lvalues): a location that values can be read from or written to. The difference between a place and a pointer is that a place is an abstract expression that exists at compile time, such as state.cache, and a pointer is one way to represent a place at run time.

The idea is to create a new handle type for every smart pointer. Often the handle will simply be a wrapper around an unsafe pointer to the same location. Then, the compiler will automatically create handles to represent places referenced by the program. That allows user code to implement traits on a handle type that will affect how the borrow checker interacts with handles which correspond to a custom smart pointer, with a higher degree of flexibility than the existing Deref and DerefMut traits. As an example of what that would look like, Mandry showed how a library author might go about teaching the borrow checker how to handle writes to places that are accessed via a NonNull pointer:

    unsafe impl<T> WritePlace for NonNullHandle<T> {
        const SAFE: bool = false;
        unsafe fn write_place(self, value: T) {
            unsafe { self.0.write(value) }
        }
    }

The WritePlace trait would be used to tell the borrow checker that a particular kind of handle (in this case, a handle referencing a NonNull pointer) can be written to, and how. When SAFE is set to false, the borrow checker treats writing to the associated place as an unsafe operation. The actual write is forwarded through to the raw pointer that NonNull wraps. The whole trait implementation is unsafe because an incorrect implementation of the trait could cause the borrow checker to make a mistake and result in unsound behavior.

A corresponding ReadPlace trait encodes how to read from a handle. More interesting are ProjectPlace and BorrowPlace. The former tells the borrow checker how to turn a place containing a structure into a place containing one of its fields. For example, how to turn (the handle for) a MutexGuard<RenderState> into a MutexGuard<String> when the programmer writes state.template. The latter tells the borrow checker how to create a new smart pointer that borrows a given place, the same way that & works for built-in references.

The language team is still debating what the syntax for creating a new smart pointer with BorrowPlace should be. While it could use the same & symbol, that might be confusing and make it harder to use type inference. One proposal is to use an @ symbol instead, but people aren't entirely happy with that either. No matter what syntax is eventually decided on, the BorrowPlace trait encodes all of the information that the borrow checker needs to know in order to safely handle the pointer type. This means that currently built-in behavior can be defined via the same mechanism. For example, this is what an implementation for the built-in reference type &T would look like:

    unsafe impl<'a, T> BorrowPlace<&'a T> for RefHandle<'a, T> {
        // Tell the borrow checker that multiple references can be made to the
        // same place at the same time:
        const ACCESS: AccessKind = AccessKind::Shared;
        // And that the borrow lasts for the lifetime 'a:
        type Timing = Lifetime<'a>;
        // And that creating references is a safe operation:
        const SAFE: bool = true;

        unsafe fn borrow(self) -> &'a T {
            unsafe { &*self.ptr.as_ptr() }
        }
    }

That gives users an example of how to write their own smart pointers that act like references, and gives the standard library maintainers a place to document existing counterintuitive built-in behaviors. With a similar implementation of a handle type for MutexGuard, the earlier render_page() example works without errors. Mandry called it "a small code change for this example, but a big semantic shift."

Tying smart pointers more closely to the compiler's existing internals has other benefits, however. Currently, it is possible to use pattern matching on a value behind a reference, but not on a value behind a smart pointer — at least without dereferencing the pointer and reborrowing the value behind it. The details exposed by BorrowPlace would be enough to let the compiler safely implement those pattern matches.

One of the criteria that the language team looks for in new features is composability, Mandry said: how well the prospective feature integrates into the existing structures of the language, and how well multiple uses of the feature compose with each other. Handles and places "compose beautifully" because it is just exposing some details of how the compiler already understands and processes the language.

Despite that, exposing places and handles to library code is still a prototype. Mandry asked for help ensuring that the design would work for everyone's use cases; he asked the audience to look at the design documentation and add their own examples of smart pointers with weird semantics. "If you have an abstraction that you want to have more deeply integrated into the language, please help us out by trying this, and telling us about any roadblocks."

The next part of the design that he intends to flesh out is errors and diagnostic messages. Ideally, users should never see errors mentioning the newly added traits; those would remain internal, and the error messages would explain the problem directly, as they do for built-in references. Even though there is much more work to do before it can become a stable part of the language, Mandry is optimistic about this design. "My hope is that it will let libraries make Rust even more powerful and friendly."

One audience member wanted to know whether this design would also support destructive pattern matching (a combined operation that takes ownership of a value while matching it against possible patterns, pulling it apart into its component pieces). Mandry said that it would, as long as the developer implemented a VariantPlace trait for the appropriate handle. There are six to eight operations to implement to support every operation on a handle type, and that's one of them, he said.

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.

[ Thanks to the Linux Foundation, LWN's travel sponsor, for assistance in traveling to Montreal for RustConf. ]


Index entries for this article
ConferenceRustConf/2026


to post comments

Projecting Option fields

Posted Oct 2, 2026 19:22 UTC (Fri) by NYKevin (subscriber, #129325) [Link] (5 responses)

> 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> = ...;
let bar: Option<&Bar> = foo.as_ref().and_then(|x| x.bar);

It would be much nicer if we could just do this:

let foo: Option<Foo> = ...;
let bar: Option<&Bar> = &foo.bar;

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) by matthias (subscriber, #94967) [Link] (4 responses)

And it would be really nice if we could to this:

let foo: Option<Foo> = ...;
let bar: Option<&mut Bar> = &mut foo.bar;
let bat: Option<&mut Bat> = &mut foo.bat;

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) by mathstuf (subscriber, #69389) [Link] (3 responses)

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.

Basically, there's a read half of an RwLock (I wish it could be so restricted, but it's only ever used for reading) and a write half of a channel. The read half of the channel uses a write lock on the same RwLock for its bits. The invariant is that the read lock must not be held when pushing into the channel because that read lock could then block the write lock from allowing the channel consumer to continue on that side therefore deadlocking on a full channel. So access to the read lock and channel writer is mediated by a &mut access to a struct containing the two. Rust therefore guarantees that the read lock is disposed of before it allows access to the channel.

Projecting Option fields

Posted Oct 4, 2026 6:35 UTC (Sun) by matthias (subscriber, #94967) [Link] (2 responses)

> , 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) by mathstuf (subscriber, #69389) [Link] (1 responses)

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.

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.

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) by matthias (subscriber, #94967) [Link]

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.

I am grateful for this important work. I know that suggesting features is easy, but implementing them in a way that they really work is hard. There are so many edge cases that need to be considered and you always have to expect that language features are combined in the weirdest possible way.

So a big thank you to all of you that question every new feature and cause delays in stabilization just to make sure that every stabilized feature is really stable and does not cause more trouble than it brings benefits.

Confusing / misleading topic.

Posted Oct 22, 2026 7:15 UTC (Thu) by ReDress (guest, #174819) [Link] (1 responses)

Hello here!

I opened this article hoping to look at the 'binary' operator '&' and read about it. By binary, I mean an operator which works / functions at binary level.

Maybe I am missing out huge on this. Maybe what is described here is functionality at binary level? Obviously not.

Yeah, would have helped a bit if the topic wasn't misleading.

And, I mean, for the lack of a better word, 'clickbait'!

Confusing / misleading topic.

Posted Oct 22, 2026 12:46 UTC (Thu) by daroc (editor, #160859) [Link]

I'm sorry you feel misled; since the actual Rust project goal is titled "Beyond the &", Mandry's talk shared the title, and the introduction to the article specifically mentions smart pointers as the topic, I don't feel that the title is particularly clickbaity.

But I am curious what you would expect an extension to the bitwise and operator to look like; if you have an idea for a topic like that, you're more than welcome to email lwn@lwn.net to suggest it.


Copyright © 2026, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds