当我可以进行可变变量绑定时,为什么需要重新绑定/阴影? [英] Why do I need rebinding/shadowing when I can have mutable variable binding?
问题描述
当我可以进行可变变量绑定时,为什么需要重新绑定/阴影?考虑:
Why do I need rebinding/shadowing when I can have mutable variable binding? Consider:
let x = a();
let x = b(x);
对比
let mut x = a();
x = b(x);
可变变量绑定允许在 this 上对该变量进行可变借用.但是阴影是否比可变绑定有一些优势?
Mutable variable binding allows a mutable borrow of that variable over this. But does shadowing have some advantages over mutable bindings?
推荐答案
因为两者的效果完全不同.
要真正了解发生了什么,我们需要从头开始:什么是绑定?绑定是什么意思?
To really understand what is going on, we need to start at the beginning: what is a binding? What does binding mean?
让我们考虑一个简单的函数:fn hello() ->字符串;
.
Let's consider a simple function: fn hello() -> String;
.
当像这样调用这个函数时:
When invoking this function like so:
fn main() {
hello();
}
发生了什么?
该函数返回一个 String
,该字符串会被立即丢弃(执行 Drop
以释放内存).
The function returns a String
, which is promptly discarded (executing Drop
as it is thereby freeing its memory).
结果被丢弃,因为它没有绑定到变量名,语言规则说如果没有绑定,那么它可以被及时丢弃1.
The result is dropped because it was not bound to a variable name, and the rules of the language say that if not bound then it can be promptly dropped1.
然而,如果我们绑定这个结果,我们会延长这个值的生命周期,我们可以通过这个绑定访问它......一段时间.
If we bind this result, however, we prolong the life of this value, and we can access it via this binding... for a while.
fn main() {
let value = hello();
std::mem::drop(value);
println!("{}", value); // Error: moved out of value
}
这是手头的问题:在 Rust 中,值的生命周期与绑定的范围无关.
This is the issue at hand: in Rust, the lifetime of a value is independent from the scope of a binding.
一个值在它的绑定退出其作用域之前甚至不需要被删除:它可以转移到另一个(类似于从函数返回).
A value need not even be dropped before its binding exits its scope: it can be transferred to another (similar to returning from a function).
fn main() {
let x;
{
let y = hello();
x = y;
}
println!("{}", x);
}
1 如果绑定到 _
也会发生同样的情况.
1 the same happens if binding to _
.
所以,现在我们已经了解绑定和值不同的事实,让我们检查两个片段.
So, now we armed with the fact that bindings and values differ, let's examine the two snippets.
第一个阴影片段,与您的不同:
A first shadowing snippet, differing from yours:
fn main() {
let x = a();
let x = b();
}
步骤,按顺序:
- 表达式
a()
创建一个值,该值绑定到x
- 表达式
b()
创建一个值,该值绑定到x
b()
创建的值被丢弃a()
创建的值被丢弃
- The expression
a()
creates a value, which is bound tox
- The expression
b()
creates a value, which is bound tox
- The value created by
b()
is dropped - The value created by
a()
is dropped
请注意,重新绑定 x
的事实不会影响先前绑定的值的生命周期.
Note that the fact that x
is re-bound does not affect the lifetime of the value that was previously bound.
从技术上讲,它的行为与 b()
的结果绑定到 y
完全一样,唯一的例外是前面的 x
y
在范围内时无法访问绑定.
Technically, it behaves exactly as if the result of b()
was bound to y
, with the sole exception that the previous x
binding is not accessible while y
is in scope.
现在,可变片段:
fn main() {
let mut x = a();
x = b();
}
步骤,按顺序:
- 表达式
a()
创建一个值,该值绑定到x
- 表达式
b()
创建一个值,该值绑定到x
,以及前一个值(由a()
创建)被丢弃 b()
创建的值被丢弃
- The expression
a()
creates a value, which is bound tox
- The expression
b()
creates a value, which is bound tox
, and the previous value (created bya()
) is dropped - The value created by
b()
is dropped
再一次,访问前一个值是不可能的,但是使用阴影是暂时不可能的(如果在较小的范围内进行阴影),使用赋值是不可能永远的,因为值被删除了.
Once again, accessing the previous value is impossible, however whilst with shadowing it's impossible temporarily (if shadowing in a smaller scope), with assignment it's impossible forever since the value is dropped.
这篇关于当我可以进行可变变量绑定时,为什么需要重新绑定/阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!