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.
  • 相关阅读:
    里氏代换原则
    依赖倒转原则
    开放-封闭原则
    如何判断对象是否死亡和类是无用的类
    Java内存区域
    Zookeeper使用场景
    zookeeper知识点总结
    前端小技术总结
    lambda表达式的使用
    Comparator进行List集合排序
  • 原文地址:https://www.cnblogs.com/dream397/p/14185066.html
Copyright © 2011-2022 走看看