Rust- 变量绑定

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:

rust 复制代码
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:

rust 复制代码
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:

rust 复制代码
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:

rust 复制代码
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.

rust 复制代码
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.

rust 复制代码
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);
}
相关推荐
Arman_1 小时前
02 rusty-cat 实战:MeowClient 配置、任务参数、进度回调与暂停恢复
http·https·rust·tokio·文件分片上传·文件分片下载
wzhao1012 小时前
Relink 0.15.1:一个 no_std 的 ELF 加载器/链接器
linux·rust·gnu
yzwlord3 小时前
【无标题】
linux·运维·rust·ssh
Arman_4 小时前
Rust 客户端安全上传下载阿里云 OSS:rusty-cat 预签名 URL 实战
安全·阿里云·rust·oss断点续传
灵机一物4 小时前
灵机一物AI原生电商小程序、PC端(已上线)-【技术深度解析】Bun 6 天 AI 重写 96 万行代码:从 Zig 迁移 Rust 全流程与行业影响
开发语言·人工智能·rust
Arman_4 小时前
03 rusty-cat 进阶解析:架构设计、云存储接入、安全模型与长期维护评估
css·安全·rust·文件分片上传·文件分片下载
techdashen4 小时前
半小时读懂 Rust:从语法符号到所有权思维
开发语言·rust
星栈4 小时前
Rust 全栈 SSR 用了一年,我踩过的 5 个坑和 3 个真香瞬间
rust·开源·全栈
Rust研习社4 小时前
手把手带你使用 Bacon 高效开发应用
后端·rust·编程语言
Arman_6 小时前
Rust 客户端安全上传下载微软 Azure Blob:rusty-cat SAS 预签名实战
安全·microsoft·rust·azure·断点续传