Deref desugaring rules (&*val)

Let’s say we have the following code:

let a = 0u32;
let b = &a;
let c = &*b; // Not a reference of a temporary!
assert_eq!(b, c);

What are the exact rules here? Why doesn’t this actually create a temporary? Does &* always have the same semantics as shown in the code above?

1 Like

You’re an imposter! Mr. borrowck would know this, on account of being the borrow checker!

But to answer your question, whoever you are: because *b is of type u32, but is not a u32 value. That is, the compiler distinguishes internally between “this is a u32 value” and “this is the location of a u32 value”. *b is the second one. Because it’s a location, it can re-reference it without having to copy the value out first.

A real temporary (like the result of a function call, or a literal, or the result of addition… any value which doesn’t have a storage location) would be the first kind.


This is a mere coincident, clearly my name is Borrow Ck.

I see. However, can I read this up somewhere? (Not that I don’t trust you). Are there any references documenting the exact rules here?

Looks like it’s in Place Expressions and Value Expressions.

(As an aside: I love it when web pages mess with copy+paste, making it impossible to just copy a piece of text for a link. So convenient!)

1 Like