A read only no alias reference
Rust Internals [Unofficial]
June 24, 2026
It is already possible to do that:
pub struct ExclusiveReadOnly<'a, T: ?Sized>(&'a mut T);
impl<'a, T: ?Sized> ExclusiveReadOnly<'a, T> {
pub fn new(r: &'a mut T) -> Self {
Self(r)
}
}
impl<T: ?Sized> std::ops::Deref for ExclusiveReadOnly<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.0
}
}
Compiler optimizations will treat this struct like &mut T, but it's impossible to write into it.
However this doesn't enable any additional optimizations, in fact it enables less: &T is already noalias, and this struct will not be readonly. noalias in LLVM means that if there are writes to this memory, they will be done from this pointer only. If there are no writes, this is trivially satisfied. LLVM just doesn't have any attribute for "readonly exclusive pointer", and I doubt such attribute has any optimization utility. Furthermore, the current memory models proposed for Rust also have no such thing, so it will require extending them for very dubious benefit.
Discussion in the ATmosphere