一个static
变量保证只有一个实例,你可以取其引用。一个const
变量不具有这个保证,编译器允许它有零个、一个或多个实例。
在你的情况下,代码等同于:
println!("{}", AtomicUsize::new(0).load(Ordering::SeqCst));
println!("{}", AtomicUsize::new(0).fetch_add(10, Ordering::SeqCst));
println!("{}", AtomicUsize::new(0).load(Ordering::SeqCst));
每个值都是创建并丢弃,因此一个值的更改不会传播到另一个值。
在某些方面,可以将const
变量视为C或C++#define
——从概念上讲,该值只是粘贴在使用它的任何地方。
Clippy 0.0.211有一个针对这种情况的lint:
error: a const item should never be interior mutable
--> src/main.rs:3:1
|
3 | const SOME_VAR: AtomicUsize = AtomicUsize::new(0);
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`
|
= note: #[deny(declare_interior_mutable_const)] on by default
= help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.211/index.html#declare_interior_mutable_const
error: a const item with interior mutability should not be borrowed
--> src/main.rs:6:20
|
6 | println!("{}", SOME_VAR.load(Ordering::SeqCst));
| ^^^^^^^^
|
= note: #[deny(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-nursery.github.io/rust-clippy/v0.0.211/index.html#borrow_interior_mutable_const
在Java中,我可以使用一个
final HashMap
。
是的,在Java中非常容易创建一个
非线程安全的
HashMap
。Rust不想让创建可能导致内存不安全的代码变得容易。您需要使用适当的安全机制来保护类型,例如使用
Mutex
,或者如果您作为程序员保证全局值仅由一个线程使用,则需要使用
unsafe
代码。
另请参见:
-
为什么const变量的更改在使用之间不会持久存在?
-
如何创建全局可变单例?
-
如何在Rust中创建安全的静态单例?
final
,因为它不支持继承并且变量默认为不可变。Java 没有类似于 Rust 的const
,因为 Java 倾向于依赖 JVM 来进行优化,而不是在编译时进行评估。Rust 的const
更像 C++ 的constexpr
,而 Java 的final
更像 C++ 的const
。一清二楚! - trent