关闭

边做边学Rust之变量绑定

标签: Rust语言golinuxrust安全
568人阅读 评论(0) 收藏 举报
分类:

4 变量绑定

Rust通过静态类型实现类型安全。变量绑定可以在定义时注明类型。然而在大多数情况下,编译器可以从上下文环境中推断出变量类型,大大的减轻了注解的负担。


使用let,可以将值(例如literals)绑定到变量。

fn main() {
    let an_integer = 1u32;
    let a_boolean = true;
    let unit = ();

    // copy `an_integer` into `copied_integer`
    let copied_integer = an_integer;

    println!("An integer: {:?}", copied_integer);
    println!("A boolean: {:?}", a_boolean);
    println!("Meet the unit value: {:?}", unit);

    // The compiler warns about unused variable bindings; these warnings can
    // be silenced by prefixing the variable name with an underscore
    let _unused_variable = 3u32;

    let noisy_unused_variable = 2u32;
    // FIXME ^ Prefix with an underscore to suppress the warning
}

程序运行结果:


An integer: 1
A boolean: true
Meet the unit value: ()

4.1 可变性


变量绑定默认是不可变的,但是可以使用mut关键字覆盖:


fn main() {
    let _immutable_binding = 1;
    let mut mutable_binding = 1;

    println!("Before mutation: {}", mutable_binding);

    // Ok
    mutable_binding += 1;

    println!("After mutation: {}", mutable_binding);

    // Error!
    //_immutable_binding += 1;
    // FIXME ^ Comment out this line
}

程序执行结果:


Before mutation: 1
After mutation: 2

4.2 作用于和覆盖


变量绑定有一个作用域,被限制在一个块中。块是一个被{}包括的集合。并且允许变量覆盖。

fn main() {
    // This binding lives in the main function
    let long_lived_binding = 1;

    // This is a block, and has a smaller scope than the main function
    {
        // This binding only exists in this block
        let short_lived_binding = 2;

        println!("inner short: {}", short_lived_binding);

        // This binding *shadows* the outer one
        let long_lived_binding = 5_f32;

        println!("inner long: {}", long_lived_binding);
    }
    // End of the block

    // Error! `short_lived_binding` doesn't exist in this scope
    //println!("outer short: {}", short_lived_binding);
    // FIXME ^ Comment out this line

    println!("outer long: {}", long_lived_binding);

    // This binding also *shadows* the previous binding
    let long_lived_binding = 'a';

    println!("outer long: {}", long_lived_binding);
}

程序运行结果:


inner short: 2
inner long: 5
outer long: 1
outer long: a

4.3 声明在先


可以先声明变量绑定,后初始化。然而这种形式很少使用,因为它可能导致使用未初始化的变量。

fn main() {
    // Declare a variable binding
    let a_binding;

    {
        let x = 2;

        // Initialize the binding
        a_binding = x * x;
    }

    println!("a binding: {}", a_binding);

    let another_binding;

    // Error! Use of uninitialized binding
    //println!("another binding: {}", another_binding);
    // FIXME ^ Comment out this line

    another_binding = 1;

    println!("another binding: {}", another_binding);
}

程序运行结果:
a binding: 4
another binding: 1

编译器禁止使用未初始化的变量,因为这可能导致未定义的操作。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:205342次
    • 积分:4048
    • 等级:
    • 排名:第7659名
    • 原创:186篇
    • 转载:17篇
    • 译文:14篇
    • 评论:31条
    博客专栏
    最新评论
    process
    LPI-2.6

    WEB-day01-4