• 普通人学习rust——从零到放弃 变量、不可变量、常量


    普通人学习rust——从零到放弃 变量、不可变量、常量

    环境

    本文章内容基于如下环境,如若出入请参考当前环境。

    rustc 1.42.0 (b8cedc004 2020-03-09)
    
    cargo 1.42.0 (86334295e 2020-01-31)
    

    前言

    可变变量、不可变量、常量 是rust语言的一个特性。本篇文章主要讲rust的三个关键字:letmutconst

    不可变量

    let 是创建变量的关键字,rust默认创建的变量为不可变量。吐槽:大多数情况下,程序都是使用可变变量,而rust却默认创建的不可变的变量,纯粹为了特别而特别吧!

    //设置不可变量
        let x = 5;
    	println!("let x={}",x);
    

    不可变量不可对变量进行修改,错误示例

    	//设置不可变量
        let x = 5;
    	println!("let x={}",x);
    	//不可以对不可变量进行修改
    	x = 6;
    	println!("let x={}",x);
    

    编译错误

    error[E0384]: cannot assign twice to immutable variable `x`
     --> main.rs:6:2
      |
    3 |     let x = 5;
      |         -
      |         |
      |         first assignment to `x`
      |         help: make this binding mutable: `mut x`
    ...
    6 |     x = 6;
      |     ^^^^^ cannot assign twice to immutable variable
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0384`.
    

    不可变量可以重新定义,重新定义包括改变类型

    正确示例

    	//设置不可变量
        let x = 5;
    	println!("let x={}",x);
    	//重新定义
    	let x = 6;
    	println!("let x={}",x);
    	//重新定义,并改变类型
    	let x = "字符串";
    	println!("let x={}",x);
    

    运行结果

    let x=5
    let x=6
    let x=字符串
    

    可变变量

    绝大多数情况下,程序都是定义为可以修改的变量,变量本来就是要变的,只是rust加了一层不可变而已。rust下需要加上mut才能使变量可以修改。

    正确示例

    	//设置可变量
        let mut x = 5;
    	println!("let x={}",x);
    	//修改值
    	x = 6;
    	println!("let x={}",x);
    

    运行结果

    let x=5
    let x=6
    

    修改值需要保持类型一致,错误示例

    	//设置不可变量
        let mut x = 5;
    	println!("let x={}",x);
    	//修改值
    	x = "字符串";
    	println!("let x={}",x);
    

    编译错误

    error[E0308]: mismatched types
     --> main.rs:6:6
      |
    6 |     x = "字符串";
      |         ^^^^^^^^ expected integer, found `&str`
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0308`.
    

    常量

    定义常量使用const作为关键字。

    	//设置常量
        const PI:f32 = 3.14;
    	println!("const PI={}",PI);
    

    运行结果

    const PI=3.14
    

    常量不可修改,这跟不可变量是一样的
    常量不可重新定义,错误示例1

    fn main(){
    	//设置常量
        const PI:f32 = 3.14;
    	println!("const PI={}",PI);
    	const PI:f32 = 3.1415;
    	println!("const PI={}",PI);
    }
    

    编译错误

    error[E0428]: the name `PI` is defined multiple times
     --> main.rs:5:2
      |
    3 |     const PI:f32 = 3.14;
      |     -------------------- previous definition of the value `PI` here
    4 |     println!("const PI={}",PI);
    5 |     const PI:f32 = 3.1415;
      |     ^^^^^^^^^^^^^^^^^^^^^^ `PI` redefined here
      |
      = note: `PI` must be defined only once in the value namespace of this block
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0428`.
    

    错误示例2

    //设置常量
        const PI:f32 = 3.14;
    	println!("const PI={}",PI);
    	let PI:f32 = 3.1415;
    	println!("let PI={}",PI);
    

    编译错误

    warning: floating-point types cannot be used in patterns
     --> main.rs:5:6
      |
    5 |     let PI:f32 = 3.1415;
      |         ^^
      |
      = note: `#[warn(illegal_floating_point_literal_pattern)]` on by default
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
    
    error[E0005]: refutable pattern in local binding: `_` not covered
     --> main.rs:5:6
      |
    3 |     const PI:f32 = 3.14;
      |     -------------------- constant defined here
    4 |     println!("const PI={}",PI);
    5 |     let PI:f32 = 3.1415;
      |         ^^
      |         |
      |         interpreted as a constant pattern, not a new variable
      |         help: introduce a variable instead: `pi_var`
    
    warning: floating-point types cannot be used in patterns
     --> main.rs:5:6
      |
    5 |     let PI:f32 = 3.1415;
      |         ^^
      |
      = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
      = note: for more information, see issue #41620 <https://github.com/rust-lang/rust/issues/41620>
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0005`.
    

    常量不能推导类型需要确定类型,必须明确类型。吐槽:推导类型应该不难吧,纯粹就是为了瘸一班的感觉!

    错误示例

    //设置常量
        const PI = 3.14;
    	println!("const PI={}",PI);
    

    编译错误

    error: missing type for `const` item
     --> main.rs:3:11
      |
    3 |     const PI = 3.14;
      |           ^^ help: provide a type for the item: `PI: f64`
    
    error: aborting due to previous error
    

    常量可以在函数外部定义,而可变量和不可变量不可以。
    正确示例

    //设置常量
    const PI:f32 = 3.14;
    fn main(){
    	println!("const PI={}",PI);
    }
    

    运行结果

    const PI=3.14
    

    错误示例

    //设置
    let PI:f32 = 3.14;
    fn main(){
    	println!("const PI={}",PI);
    }
    

    编译错误

    error: expected item, found keyword `let`
     --> main.rs:2:1
      |
    2 | let PI:f32 = 3.14;
      | ^^^ expected item
    
    error: aborting due to previous error
    

    总结

    rust分为可变变量、不可变变量、常量,默认创建的变量为不可变量。

  • 相关阅读:
    Unique Binary Search Trees(dp)
    Binary Tree Inorder Traversal
    Reverse Linked List II
    O​r​a​c​l​e​1​1​g​自​带​的​S​Q​L​ ​d​e​v​e​l​o​p​e​r​无​法​打​开​解​决​
    英语飙升的好方法
    MyEclipse加入jquery.js文件missing semicolon的错误
    js,jq获取手机屏幕分辨率的宽高
    给标签元素设固定宽高,内部添加滚动条显示
    解决手机端点击input的时候,页面会放大
    支付宝异步回调验证签名
  • 原文地址:https://www.cnblogs.com/loveheihei/p/12551962.html
Copyright © 2020-2023  润新知