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);
}
相关推荐
Flynt6 小时前
OpenAI把Codex底层从TS重写成Rust,我拆开看了下它的三层架构
rust·openai·agent
第一程序员7 小时前
AI 辅助编程的 7 个误区:把模型当高级搜索引擎是对它的最大浪费
python·rust·github
NutShell Wang12 小时前
Firecrawl anydoc 实战拆解:用一个 Rust 依赖吃下 14 种文档格式
性能优化·rust·vibe coding
DevHub12 小时前
probe-rs 教程:一行命令替代 OpenOCD 烧写调试 Cortex-M
arm开发·嵌入式硬件·mcu·rust
程序员爱钓鱼12 小时前
Rust const泛型详解:让常量也成为泛型参数
后端·面试·rust
k4m7v2pz13 小时前
ESP32-S3 + I2S 麦克风实时音频流静音问题排查:从网络到AGC的谬误溯源
rust·音频·esp32·agc·实时监听·静音排查
SomeB1oody14 小时前
【RustyML入门】7.4. 按需裁剪与模块化集成
开发语言·后端·机器学习·rust·教程
Source.Liu14 小时前
【A11】基于 PDF 渲染引擎的统一文档界面架构设计
rust·pdf
SomeB1oody18 小时前
【RustyML入门】7.3. 性能调优与并行
开发语言·后端·机器学习·rust·教程
苏灿烤鱼18 小时前
连庄+2,729,空降榜眼只+440
rust·openai·agent