Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

常量项

Syntax
ConstantItem
    const ( IDENTIFIER | _ ) : Type ( = Expression )? ;

常量项是一个可选的带名称的*常量值*,不与程序中的特定内存位置关联。

常量本质上在使用的每个地方都会被内联,这意味着在使用时它们会被直接复制到相关上下文中。这包括使用来自外部 crate 的常量,以及非 Copy 类型。对同一常量的引用不一定保证引用相同的内存地址。

常量声明在其所在模块或块的值命名空间中定义常量值。

常量必须显式指定类型。该类型必须具有 'static 生命周期:初始化器中的任何引用都必须具有 'static 生命周期。常量类型中的引用默认为 'static 生命周期;请参见静态生命周期省略

如果常量值符合提升的条件,对常量的引用将具有 'static 生命周期;否则会创建一个临时值。

#![allow(unused)]
fn main() {
const BIT1: u32 = 1 << 0;
const BIT2: u32 = 1 << 1;

const BITS: [u32; 2] = [BIT1, BIT2];
const STRING: &'static str = "bitstring";

struct BitsNStrings<'a> {
    mybits: [u32; 2],
    mystring: &'a str,
}

const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
    mybits: BITS,
    mystring: STRING,
};
}

常量表达式只能在 trait 定义中省略。

带析构函数的常量

常量可以包含析构函数。析构函数在值离开作用域时运行。

#![allow(unused)]
fn main() {
struct TypeWithDestructor(i32);

impl Drop for TypeWithDestructor {
    fn drop(&mut self) {
        println!("Dropped. Held {}.", self.0);
    }
}

const ZERO_WITH_DESTRUCTOR: TypeWithDestructor = TypeWithDestructor(0);

fn create_and_drop_zero_with_destructor() {
    let x = ZERO_WITH_DESTRUCTOR;
    // x 在函数末尾被释放,调用 drop。
    // 打印 "Dropped. Held 0."。
}
}

未命名常量

关联常量不同,自由常量可以通过使用下划线代替名称来取消命名。例如:

#![allow(unused)]
fn main() {
const _: () =  { struct _SameNameTwice; };

// OK 尽管名称与上面相同:
const _: () =  { struct _SameNameTwice; };
}

下划线导入一样,宏可以安全地在同一作用域中多次发出相同的未命名常量。例如,以下不应产生错误:

#![allow(unused)]
fn main() {
macro_rules! m {
    ($item: item) => { $item $item }
}

m!(const _: () = (););
// 这会展开为:
// const _: () = ();
// const _: () = ();
}

求值

自由常量总是在编译时被求值以显示 panic。即使在未使用的函数中也会发生:

#![allow(unused)]
fn main() {
// 编译时 panic
const PANIC: () = std::unimplemented!();

fn unused_generic_function<T>() {
    // 一个失败的编译时断言
    const _: () = assert!(usize::BITS == 0);
}
}