gpt4 book ai didi

variables - 常量和静态变量有什么区别,我应该选择哪个?

转载 作者:行者123 更新时间:2023-11-29 07:52:05 26 4
gpt4 key购买 nike

我从 RFC 246 知道这个:

  • constants declare constant values. These represent a value, not a memory address. This is the most common thing one would reach for and would replace static as we know it today in almost all cases.
  • statics declare global variables. These represent a memory address. They would be rarely used: the primary use cases are global locks, global atomic counters, and interfacing with legacy C libraries.

当我尝试维护一个表时,我不知道两者之间到底有什么不同。

我应该选择哪个?

最佳答案

可变性

Rust 中的 constant 是不可变的。您既不能重新分配也不能修改它:

struct Foo(u32);

const FOO: Foo = Foo(5);
const mut FOO: Foo = Foo(5); // illegal

fn main() {
FOO = Foo(1); //illegal
FOO.0 = 2; //illegal
}

static 变量可以是可变的,因此可以修改或重新分配。请注意,编写/修改全局 static 变量是不安全的,因此需要一个 unsafe block :

struct Foo(u32);
static FOO: Foo = Foo(5);
static mut FOO_MUT: Foo = Foo(3);

fn main() {
unsafe {
FOO = Foo(1); //illegal
FOO.0 = 2; //illegal

FOO_MUT = Foo(1);
FOO_MUT.0 = 2;
}
}

出现次数

当您编译二进制文件时,所有 const“出现”(您在源代码中使用该 const 的地方)都将直接替换为该值。

static 将在您的二进制文件中有一个专门的部分用于放置它们(BSS 部分,请参阅 Where are static variables stored in C and C++? 了解更多信息)。


总而言之,尽可能坚持const。如果不可能,因为您需要稍后在程序中使用非 const 方法初始化变量,请使用 lazy_static! .

内部可变性

虽然 conststatic 都可以使用内部可变性,但您永远不应该使用 const 来实现。这是一个例子

use std::sync::atomic::{AtomicU32, Ordering};

static STATIC: AtomicU32 = AtomicU32::new(0);
const CONST: AtomicU32 = AtomicU32::new(0);

fn print() {
println!("static: {}", STATIC.load(Ordering::Relaxed));
println!("const: {}", CONST.load(Ordering::Relaxed));
}

fn main() {
STATIC.store(3, Ordering::Relaxed);
CONST.store(3, Ordering::Relaxed);

print();
}

这可以在没有任何警告的情况下正常编译,但会导致不良行为。输出:

static: 3
const: 0

使用clippy时,会出现如下两个警告:

warning: a `const` item should never be interior mutable
--> src/main.rs:4:1
|
4 | const CONST: AtomicU32 = AtomicU32::new(0);
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| make this a static item (maybe with lazy_static)
|
= note: `#[warn(clippy::declare_interior_mutable_const)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const

warning: a `const` item with interior mutability should not be borrowed
--> src/main.rs:8:27
|
8 | println!("const: {}", CONST.load(Ordering::Relaxed));
| ^^^^^
|
= note: `#[warn(clippy::borrow_interior_mutable_const)]` on by default
= help: assign this const to a local or static variable, and use the variable here
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const

warning: a `const` item with interior mutability should not be borrowed
--> src/main.rs:13:5
|
13 | CONST.store(3, Ordering::Relaxed);
| ^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const

关于variables - 常量和静态变量有什么区别,我应该选择哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52751597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com