let重新绑定和标准分配之间有什么区别? [英] What is the difference between a let-rebinding and a standard assignment?

查看:78
本文介绍了let重新绑定和标准分配之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rust中,为了更改可变变量的值,以下示例代码中的let x = 12x = 12有什么区别?

In Rust, in order to change the value of a mutable variable, what is the difference in let x = 12 or x = 12 in the following sample code?

fn main() {
    let mut x: i32 = 8;
    {
        println!("{}", x); 
        let x = 12;  // what if change to x = 12
        println!("{}", x); 
    }
    println!("{}", x); 
    let x =  42;
    println!("{}", x); 
}

输出为8, 12, 8, 42.如果我将let x = 12更改为x = 12 ...

The output is 8, 12, 8, 42. If I change let x = 12 to x = 12 ...

fn main() {
    let mut x: i32 = 8;
    {
        println!("{}", x); 
        x = 12; 
        println!("{}", x); 
    }
    println!("{}", x); 
    let x =  42;
    println!("{}", x); 
}

输出为8, 12, 12, 42.

我知道Rust使用let来进行变量绑定,所以let x = 12是变量重新绑定,并且绑定仅在作用域内有效.但是,如何解释x = 12的功能和相应的作用域?这是变量绑定的一种吗?

I understand that Rust uses let to do variable binding, so the let x = 12 is a variable rebinding and the binding is only valid inside a scope. But how to explain the functionality of x = 12 and the corresponding scope? Is that a type of variable binding?

推荐答案

第二个let x引入了第二个绑定,该第二个绑定阴影覆盖了该块的其余部分.也就是说,有两个名为x的变量,但是您只能在let x = 12;语句之后的block语句中访问第二个变量.这两个变量不必具有相同的类型!

The second let x introduces a second binding that shadows the first one for the rest of the block. That is, there are two variables named x, but you can only access the second one within the block statement after the let x = 12; statement. These two variables don't need to have the same type!

然后,在block语句之后,第二个x不在范围内,因此您可以再次访问第一个x.

Then, after the block statement, the second x is out of scope, so you access the first x again.

但是,如果您改为编写x = 12;,则这是一个赋值表达式:x中的值将被覆盖.这不会引入新的变量,因此要分配的值的类型必须与变量的类型兼容.

However, if you write x = 12; instead, that's an assignment expression: the value in x is overwritten. This doesn't introduce a new variable, so the type of the value being assigned must be compatible with the variable's type.

如果您编写循环,则此区别很重要.例如,考虑以下功能:

This difference is important if you write a loop. For example, consider this function:

fn fibonacci(mut n: u32) -> u64 {
    if n == 0 {
        return 1;
    }

    let mut a = 1;
    let mut b = 1;

    loop {
        if n == 1 {
            return b;
        }

        let next = a + b;
        a = b;
        b = next;
        n -= 1;
    }
}

此函数重新分配变量,以便循环的每次迭代都可以对前一次迭代中分配的值进行操作.

This function reassigns variables, so that each iteration of the loop can operate on the values assigned on the preceding iteration.

但是,您可能会想像这样编写循环:

However, you might be tempted to write the loop like this:

loop {
    if n == 1 {
        return b;
    }

    let (a, b) = (b, a + b);
    n -= 1;
}

这不起作用,因为let语句引入了新变量,并且这些变量在下一次迭代开始之前将超出范围.在下一次迭代中,(b, a + b)仍将使用原始值.

This doesn't work, because the let statement introduces new variables, and these variables will go out of scope before the next iteration begins. On the next iteration, (b, a + b) will still use the original values.

这篇关于let重新绑定和标准分配之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆