Why doesn't this value go up to the lvalue value as indicated in the link?

Rust Reference says:

The left operand of an assignment expression or compound assignment is an lvalue context, as is the only unary borrow operand.

[...]

When the rvalue value is used in the context of an lvalue, a temporary unnamed lvalue is created and used.

This rvalue promotion obviously works with borrowing:

let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created 

But it does not work in the task (although the link talks about all lvalue contexts, not just borrowing):

 27 = 28; // error[E0070]: invalid left-hand side expression 

error description E0070 does not mention this rvalue advertisement. Is this a mistake in the link, or is there really some way to initiate the rvalue promotion using assignment expressions or compound assignments?

There is a third type of lvalue context that also incorrectly describes the link. Whenever there is a template with ref , binding the left value to this template represents the lvalue context. It turns out that promotion works in this case:

 let ref x = 3; // works 

So, apparently, promotion just doesn't work for (composite) appointments?

+5
source share
1 answer

The link has been updated since the publication of this question. Now he says that the rvalue for promoting the lvalue does not happen at the time of the assignment, so this is apparently an error in the old link.

Borrow operators :

If the and or am mut operators are applied to the r-value, a temporary value is created

Perhaps this also applies to ref bindings, although I do not see any explicit mention in it.

Assignment :

The left operand must be an lvalue: using rvalue results in a compiler error, not a temporary one.

+1
source

Source: https://habr.com/ru/post/1262456/


All Articles