6

Is is possible to define a global constant, value of which is computed at the beginning of the runtime? Something like

static START_TIME: time::Timespec = time::get_time();

if it were possible. static and const declaration requires compile time value (calls in constants are limited to struct and enum constructors) and let can't be put outside function (error: expected item, found `let`).

czerny
  • 15,090
  • 14
  • 68
  • 96

1 Answers1

8

I think something like lazy_static can help with this.

Ankur
  • 33,367
  • 2
  • 46
  • 72
  • 1
    Since this answer 3 years passed... There is still no better way how to do this? Can't believe Rust cannot make this without using external crates. – Fusion Sep 28 '20 at 19:54
  • 2
    @Fusion there's now const fns which handle a certain number of cases. On nightly there is https://doc.rust-lang.org/std/lazy/struct.SyncLazy.html – nqe Jan 09 '22 at 18:09