在 Rust 中在运行时初始化全局常量?

cze*_*rny 4 constants global-variables rust

是否可以定义一个全局常量,其值在运行时开始计算?就像是

static START_TIME: time::Timespec = time::get_time();
Run Code Online (Sandbox Code Playgroud)

如果可能的话。staticandconst声明需要编译时值 ( calls in constants are limited to struct and enum constructors) 并且 let 不能放在函数 ( error: expected item, found `let`)之外。

Ank*_*kur 5

我认为像lazy_static这样的东西可以帮助解决这个问题。

  • @Fusion 现在有 const fns 可以处理一定数量的情况。每晚有 https://doc.rust-lang.org/std/lazy/struct.SyncLazy.html (2认同)