{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreidscoakpzpmvxcrqjyxisr53rba5wiiz32xtw3jxmwghyg57dz3vu",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mlr3al76phz2"
},
"path": "/t/instant-past-value-constant/24297#post_7",
"publishedAt": "2026-05-13T18:03:05.000Z",
"site": "https://internals.rust-lang.org",
"textContent": "The most common use-case I have is if I'm setting up a struct where, whenever a method gets called, it needs to do something if it hasn't recently enough. So I end up with something like this:\n\n\n pub struct FooDoer {\n last_bar_timestamp: Instant,\n ..\n }\n\n impl FooDoer {\n pub fn do_foo(&mut self) {\n if self.last_bar_timestamp.elapsed() >= Duration::from_secs(1) { self.do_bar(); }\n ..\n }\n fn do_bar(&mut self) {\n self.last_bar_timestamp = Instant::now();\n ..\n }\n }\n\n\nBut then, when I go to construct a `FooDoer`, then I have a few options for how to handle constructing the timestamp:\n\n 1. I can use `Instant::now()`, which has a syscall and so it's impossible to do it at const-time (and maybe subtract the interval if I need it to happen on the first time).\n 2. I can use `None` to accurately indicate that it hasn't happened yet, but then I have to rewrite everything to handle an option that will only be an option on the first time.\n 3. I can use something like the unsafe casting GP mentioned to construct an `Instant` which I know is valid and in the past, but then I'm going against the stdlib public API and that's liable to be broken on an update.\n\n\n\nAnd also, it often doesn't matter much when exactly the first call to `do_bar()` happens, so I could get around all of that if I just had a constant which is guaranteed to not be in the future, hence why I suggested just that as the guarantee.",
"title": "`Instant::PAST_VALUE` constant?"
}