Warning
This post was published 31 days ago. The information described in this article may have changed.
Disclaimer: I know I probably should not be bothered by learning this at an early stage but I think it will make reading the Documentation and reference easier.
===========
In expressions, it says:
The following contexts are place expression contexts:
- The left operand of a compound assignment expression.
And in that link there is
let mut x = 5;
x += 1;
assert!(x == 6);
let myref = &7; // I added this line to learn
There, the first line is a let
declaration statement where x
is an irrefutable pattern and 5 a value expression. The second line is a place expression. I take that assert
s argument here is also a place expression, and that all comparison like ==
and update operators like -=
would also turn that into a place expression.)
The last one (last line above) is the borrow operator applied to a value expression.
Though x
is also a variable, as in these defs:
A variable is a component of a stack frame, either a named function parameter, an anonymous temporary, or a named local variable.
A local variable (or stack-local allocation) holds a value directly, allocated within the stackโs memory. The value is a part of the stack frame.
But it is also path (and does not say in the document above that all variables are paths):
Two examples of simple paths consisting of only identifier segments:
x; x::y::z;
That's fine if something has two aspects, but is this the case ? Am I analysing correctly that x
is a variable and also a path and only the latter is needed to be place expression?
The reason why I think it is not a place expression it's because I see x
not as representing a memory location (which is the first definition of place expression) but representing a value and I only see references to x
as place expressions, that is &x
(for example.)
=====
I did find a tiny bug in the docs here (bold): https://doc.rust-lang.org/reference/expressions/operator-expr.html:
When applied to a place expression, this expressions produces a reference (pointer) to the location that the value refers to.
I'm also unsure whether the comment here is correct
{ // a temporary with value 7 is created that lasts for this scope. let shared_reference = &7; }
3 posts - 2 participants
๐ท๏ธ rust_feed