I'm trying to learn Rust, I've decided to start working on a simple API using Actix.
What I did was create a root endpoint that would return some basic info about the app (name, version, uptime).
Trying to generate current uptime is giving me a headache. At first I tried to create a constant, but I found out I cannot call non-const fn SystemTime::now in constants calls. After doing some research I found out I should be using Lazy to solve this problem.
This fixed the compile issue, but the problem now is that the uptime value is always 0. I'm guessing the reason for this is that Lazy always executes this method when called, which I do not want. How do I solve this?
This is my current implementation, essentially I want STARTUP_TIME to be initialized on app startup:
If you think about it, putting the startup time into a const doesn't make sense. A const is evaluated at compile-time, thus if you managed to do this, you would get the timestamp of the compilation. (There's another good reason why SystemTime::now() isn't const in the first place: it calls out to the OS, so evaluating it at compile time is at best problematic, at worst impossible.)
Putting the thing in a static STARTUP_TIME: Lazy<SystemTime> as suggested above will only get you part of the way. There's still the problem that a Lazy is, well, lazy… therefore, it will not evaluate until the first time you query it. Thus, you should force it to evaluate the initializer upon startup explicitly, e.g. by accessing it in main() before you do anything else.