]> git.ralfj.de Git - web.git/commitdiff
place expression blog post: add note on deref expressions master
authorRalf Jung <post@ralfj.de>
Fri, 26 Dec 2025 17:45:31 +0000 (18:45 +0100)
committerRalf Jung <post@ralfj.de>
Fri, 26 Dec 2025 17:45:31 +0000 (18:45 +0100)
personal/_posts/2024-08-14-places.md

index 4b93ee5e1dd1ac1083fa935d5952de785ade3d1a..0f442c7b70ef173371f18d58d80208188d81e70f 100644 (file)
@@ -74,9 +74,11 @@ However, the expression `my_var` (referencing a local variable), according to th
 This is because `my_var` actually denotes a place in memory, and there's multiple things one can do with a place:
 one can load the contents of the place from memory (which produces a value), one can create a pointer to the place (which also produces a value, but does not access memory at all),
 or one can store a value into this place (which in Rust produces the `()` value, but the side-effect of changing the contents of memory is more relevant).
-Besides local variables, the other main example of a place expression is the result of the `*` operator, which takes a *value* (of pointer type) and turns it into a place.
+Besides local variables, the other main example of a place expression is the result of the `*` operator, which takes a *value* (of pointer type) and turns it into a place.[^deref]
 Furthermore, given a place of struct type, we can use a field projection to obtain a place just for that field.
 
+[^deref]: **Update (2025-12-26):** If you check the [Rust reference](https://doc.rust-lang.org/reference/expressions.html#r-expr.place-value.place-context), you may notice that it actually says that `*` takes a *place* expression. This is a somewhat peculiar Rust design choice related to custom smart pointers implementing the `Deref` trait, and related to borrow checking. That turns out to be easier if you only dereference places. However, when it comes to the operational semantics, the overall picture is much cleaner if we say that `*` works on values.
+
 This may sound odd, because it means that `let new_var = my_var;` is not actually a valid statement in our grammar!
 To accept this code, the Rust compiler will automatically convert this statement into a form that fits the grammar by adding `load` whenever needed.[^desugar]
 `load` takes a place and, as the name indicates, performs a load from memory to obtain the value currently stored in this place.