rust abc(5): 常量

本文介绍了Rust中常量的基本用法,包括const的定义方式,如constPI:f64=3.1415926,以及命名规范,常量名应使用大写字母。同时,文章强调了const与immutable的区别,const需在编译时确定值,且能在函数外部声明,而let只能在函数内部声明并可能在运行时确定值。最后,文章提醒读者const不能用于运行时计算,如constH:i32=a+232会导致编译错误。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1. 目的

学习 rust 语言中常量的使用。

2. 基本用法

2.1 说明

const 大写名字:数据类型 = 值;

例如定义数学中的 π 为常量, const PI:f64 = 3.1415926;

fn main() {
    // const 大写名字:数据类型 = 值;
    const PI:f64 = 3.1415926;
    println!("PI {}", PI);
}

2.2 运行结果

zz@Legion-R7000P% rustc f1.rs 
zz@Legion-R7000P% ./f1 
PI 3.1415926

3. 不推荐或不正确用法

3.1 不推荐用小写字母作为常量名字

fn main() {
    // 常量 g 是小写字母, rust 编译器会提示为警告
    const g:f64 = 9.78186;
    println!("g {}", g);
}

在这里插入图片描述

zz@Legion-R7000P% rustc f2.rs 
warning: constant `g` should have an upper case name
 --> f2.rs:3:11
  |
3 |     const g:f64 = 9.78186;
  |           ^ help: convert the identifier to upper case: `G`
  |
  = note: `#[warn(non_upper_case_globals)]` on by default

warning: 1 warning emitted

3.2 常量名称中含有小写字母就会报warning

fn main() {
    // 常量 Gravity 包含了小写字母, rust 编译器会提示为警告
    const Gravity:f64 = 9.78186;
    println!("Gravity {}", Gravity);
}

在这里插入图片描述

zz@Legion-R7000P% rustc f3.rs
warning: constant `Gravity` should have an upper case name
 --> f3.rs:3:11
  |
3 |     const Gravity:f64 = 9.78186;
  |           ^^^^^^^ help: convert the identifier to upper case: `GRAVITY`
  |
  = note: `#[warn(non_upper_case_globals)]` on by default

warning: 1 warning emitted

3.3 定义常量时,不指定数据类型会编译报错

fn main() {
    // 常量 G 应该指定数据类型,否则编译报错
    const G = 9.78186;
    println!("G {}", G);
}

在这里插入图片描述

zz@Legion-R7000P% rustc f4.rs
error: missing type for `const` item
 --> f4.rs:3:12
  |
3 |     const G = 9.78186;
  |            ^ help: provide a type for the constant: `: f64`

error: aborting due to previous error

4. const 和 immutable 的区别

抛开变量/常量名字的大小写,我们考虑 const 和 immutable 的区别。如下代码乍一看,好像都表达了 C/C++ 语言中的 const:

let g:f64 = 9.78186;

const G:64 = 9.78186;

4.1 const 可以在函数外声明,let 只能在函数内声明

如下是一个错误示范,会触发编译报错,原因是 let g:f64=9.78186 不能放在函数外头。
f5.rust:

let g:f64 = 9.78186;

fn main() {
    println!("hello rust");
}

在这里插入图片描述

zz@Legion-R7000P% rustc f5.rs 
error: expected item, found keyword `let`
 --> f5.rs:1:1
  |
1 | let g:f64 = 9.78186;
  | ^^^ consider using `const` or `static` instead of `let` for global variables

error: aborting due to previous error

如下是一个正确的例子, const G:f64 = 78186 可以放在函数外定义:
f6.rust:

const G:f64 = 9.78186;

fn main() {
    println!("hello rust");
    println!("G {}", G);
}

运行不会报错:

zz@Legion-R7000P% ./f6 
hello rust
G 9.78186

4.2 let 等号右侧可以是运行时确定的内容,const 等号右侧必须编译时确定

也就是说, rust 语言中的 const, 表达的是编译期就确定的值, 可以理解为 C/C++ 中的 constexpr, 而并不是等同于 C/C++ 的 const.

例如如下代码的 const H:i32 = a + 232 将导致编译报错, 而 let b = a + 232 则不会报错。
f7.rs:

fn main() {
    let a = 1;
    let b = a + 232; // ok

    const H:i32 = a + 232; // cause compile error

    println!("a = {}", a);
    println!("b = {}", b);
    println!("H = {}", H);
}

在这里插入图片描述

zz@Legion-R7000P% rustc f7.rs 
error[E0435]: attempt to use a non-constant value in a constant
 --> f7.rs:5:19
  |
5 |     const H:i32 = a + 232;
  |     -------       ^ non-constant value
  |     |
  |     help: consider using `let` instead of `const`: `let H`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0435`.

5. 总结

  1. rust 语言中的 const 表达的是 C/C++ 中的 constexpr 的含义, 是编译期确定取值,因此不能把运行期确定值的变量赋值到 const 修饰的变量上。

  2. rust 语言的 const, 可以在函数外使用, 而 let a=123 形式定义变量, 虽然是 immutable 的,但是只能在函数内使用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值