Access the process starting time everywhere

LazyLock SystemTime

If you need to access the process starting time from several functions, then instead of passing it around or instead of using the caching we could use the std::sync::LazyLock.

examples/fixed-start-time/src/main.rs

use std::sync::LazyLock;
use std::time::SystemTime;

static START_TIME: LazyLock<SystemTime> = LazyLock::new(SystemTime::now);

fn main() {
    print_start_time();
    print_start_time();

}

fn print_start_time() {
    println!("{:?}", *START_TIME);
}

Output

examples/fixed-start-time/out.txt

SystemTime { tv_sec: 1781242831, tv_nsec: 637877888 }
SystemTime { tv_sec: 1781242831, tv_nsec: 637877888 }

Related Pages

Cache the result of a function

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo