赋值或复合赋值表达式的左操作数是左值上下文,一元借位的单个操作数也是如此. The left operand of an assignment or compound-assignment expression is an lvalue context, as is the single operand of a unary borrow. [...]在左值上下文中使用右值时,将创建并使用一个临时的未命名左值.When an rvalue is used in an lvalue context, a temporary un-named lvalue is created and used instead.这种右值促销显然可以借用:This rvalue promotion obviously works with borrowing:let ref_to_i32 = &27; // a temporary i32 variable with value 27 is created但是它似乎在分配中不起作用(尽管参考文献提到的是所有左值上下文,而不仅仅是借用):But it doesn't seem to work in an assignment (although the reference speaks about all lvalue contexts, not just borrowing):27 = 28; // error[E0070]: invalid left-hand side expression E0070的错误描述没有提及该右值晋升.这是参考文献中的错误,还是确实有某种方法可以通过赋值或复合赋值表达式触发右值提升?The error description of E0070 doesn't mention this rvalue promotion. Is this a mistake in the reference or is there indeed some way to trigger rvalue promotion with assignment or compound assignment expressions?还有第三种左值上下文,该引用也描述不正确.每当其中包含ref的模式时,绑定到该模式的左值就是左值上下文.事实证明,促销在​​这种情况下有效:There is a third kind of lvalue context, which the reference describes incorrectly, too. Whenever there is a pattern with a ref in it, the left value binding to that pattern is an lvalue context. It turns out that promotion works in this case:let ref x = 3; // works显然,晋升仅不适用于(化合物)分配吗?So apparently, promotion only doesn't work for (compound-)assignments?推荐答案 参考已更新.现在,它说在分配过程中不会发生从右值到左值的提升,因此这显然是旧参考文献中的错误.The reference has been updated since the time this question was posted. Now it says that rvalue to lvalue promotion doesn't happen during assignment, so this was apparently an error in the old reference. 借入操作符: 如果&或& mut运算符应用于右值,将创建一个临时值 If the & or &mut operators are applied to an rvalue, a temporary value is created尽管我没有明确提到它,但这也可能同样适用于ref绑定.This is probably meant to apply to ref bindings as well, although I don't see it explicitly mentioned. 任务: 左侧操作数必须为左值:使用右值会导致编译器错误,而不是将其提升为临时错误. The left-hand operand must be an lvalue: using an rvalue results in a compiler error, rather than promoting it to a temporary. 这篇关于为什么未将此右值提升为参考中指定的左值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 22:11