zoukankan      html  css  js  c++  java
  • rust 可变变量

    变量绑定默认是不可变的,但加上 mut 修饰语后变量就可以改变。

    fn main() {
        let _immutable_binding = 1;
        let mut mutable_binding = 1;
    
        println!("Before mutation: {}", mutable_binding);
    
        // 正确代码
        mutable_binding += 1;
    
        println!("After mutation: {}", mutable_binding);
    
        // 错误!
        _immutable_binding += 1;
        // 改正 ^ 将此行注释掉
    }
    cargo build
       Compiling own v0.1.0 (/data2/rust/mut)
    error[E0384]: cannot assign twice to immutable variable `_immutable_binding`
      --> src/main.rs:13:5
       |
    2  |     let _immutable_binding = 1;
       |         ------------------
       |         |
       |         first assignment to `_immutable_binding`
       |         help: make this binding mutable: `mut _immutable_binding`
    ...
    13 |     _immutable_binding += 1;
       |     ^^^^^^^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0384`.
    error: could not compile `own`.
    
    To learn more, run the command again with --verbose.
  • 相关阅读:
    10月日常练习1题目描述
    普及组复赛历年考题
    9.3练习题7 子串乘积正负分类 题解
    9.3练习题6 旅行 题解
    9.3练习题4 语句解析 题解
    9.3练习题5 单词覆盖还原 题解
    unity
    矩阵快速幂
    点权和
    SCOI生日快乐
  • 原文地址:https://www.cnblogs.com/dream397/p/14185066.html
Copyright © 2011-2022 走看看