Re:Crabtime: Zig's Comptime in Rust
by Zambyte on Saturday March 22, 2025 @09:27AM
(#43445538)
Reading environment variables contradicts my mental model of pure functions. Are they just not pure functions then? Or pure-ish?
[ Parent
| on HN ]
The functions are pure but they take an input from a built-in (looks like a macro) that reads the environment variable at compile time.
Also, you only compile once, so how could you tell the difference? You could say - if it was using const fn that it's a "templated" function that depends on compile time settings.
I see, I guess if you can't set environment variables during build time that makes it pure-ish enough.
Re:Crabtime: Zig's Comptime in Rust
by vlovich123 on Saturday March 22, 2025 @04:02PM
(#43448220)
You can set environment variables in your build.rs / the user sets it like A=b cargo build.
There's two lookups that can occur, distinguishing them makes it clearer.
Looking up the value of an environment variable at runtime is not a const operation, and produces an error if you try to do it in a const fn.
Looking up the value of an environment variable during compile time _can_ be done in a const context, but it'll only happen once. The environment should be considered an input to a const fn, and that makes it "pure".
EDIT: These two operations can both be done in non-const functions too, they're different functions (well, one's a macro).