Rust- 变量绑定

Rust使用let关键字进行变量绑定,默认情况下是不可变的。若需改变值,需使用mut关键字。变量必须先初始化再使用,且支持模式匹配进行复杂绑定。变量的作用域限制在代码块内,新的声明可以遮蔽旧的变量,但不是变异。此外,Rust还强调了引用期间的资源不可变性。
摘要由CSDN通过智能技术生成

In Rust, you bind values to a variable name using the let keyword. This is often referred to as “variable binding” because it’s like binding a name to a value.

Here’s a simple example:

let x = 5;

In this example, x is bound to the value 5. By default, bindings are immutable in Rust. If you try to reassign x to a different value, you’ll get a compile-time error. If you want a binding to be mutable, you can use the mut keyword:

let mut x = 5;
x = 10; // This is okay because x is mutable

In Rust, you can also bind a variable to an expression. The expression will be evaluated, and the resulting value will be bound to the variable:

let x = 5 * 5; // x is bound to the value 25

Variable binding in Rust also allows for pattern matching, which enables more complex types of binding. For example, if you have a tuple, you can bind the individual elements of the tuple to different variables:

let (x, y) = (1, 2); // x is bound to 1, and y is bound to 2

Rust also requires that all variables be initialized before they are used, which prevents undefined behavior.

Lastly, Rust features a system of “shadowing” where a new variable can be declared with the name of a previous variable, effectively creating a new variable that “shadows” the old one.

let x = 5;
let x = x + 5; // x is now 10
let x = x * 2; // x is now 20

Each x is a new variable that shadows the previous x. This is not the same as mutation because these xs are new variables, they just happen to have the same name as the previous variable.

fn main() {
    /*
        变量是有作用域的,也就是在一个代码块中生存。
        代码块 {}, 也允许变量遮蔽。
     */

    // main 函数中
    let spend = 1;
    {
        // 只存在本代码块中
        let target = "面向对象";
        println!("内部 {}", target);    // 内部 面向对象

        // 遮蔽了外面的spend
        let spend = 2.0;
        println!("内部 {}", spend);     // 内部 2
    }

    // target在此作用域是不存在的
    // println!("外部 {}", target);
    println!("外部 {}", spend);         // 外部 1

    // 遮蔽了spend
    let spend = String::from("学习时间1小时");
    println!("外部 {}", spend);         // 外部 学习时间1小时

    let spend2;
    {
        let x = 2;
        spend2 = x * x;
    }
    println!("spend2: {}", spend2);     // spend2: 4

    let spend3;
    // println!("spend3: {}", spend3); // 报错,使用了未初始化的绑定
    spend3 = 1;
    println!("another binding spend3: {}", spend3); // another binding spend3: 1

    // 冻结 资源存在使用的引用时,在当前作用域中这一资源是不可被修改的。
    let mut spend4 = Box::new(1);
    let spend5 = &spend4;   // `spend4` is borrowed here
    spend4 = Box::new(100); // `spend4` is assigned to here but it was already borrowed
    println!("{}", spend4);
    println!("{}", spend5);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青衫客36

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值