News for hackers. Stuff that matters.
Re:Crabtime: Zig's Comptime in Rust by IshKebab on Saturday March 22, 2025 @01:52PM (#43447400)
> Rust says if the parameters are themselves constants then we promise we can evaluate this at compile time and if appropriate we will

Well exactly. "if appropriate". So like C++'s `constexpr`, Rust doesn't make any guarantees about compile-time evaluation.

Zig's `comptime` must be evaluated at compile time.

Re:Crabtime: Zig's Comptime in Rust by kibwen on Saturday March 22, 2025 @04:20PM (#43448335)
> Zig's `comptime` must be evaluated at compile time.

Yes, and the equivalent in Rust is any constant context, such as a const item, or a const block inside of a non-const function. Anything in a constant context is guaranteed to run at compile-time.

Re:Crabtime: Zig's Comptime in Rust by tialaramex on Saturday March 22, 2025 @06:00PM (#43448979)
If we want a constant context, we can say so. Because Rust is expression oriented we can write for example a loop (though if you're unfamiliar with Rust it may not be clear why a for loop can't work yet, other loops are fine) and wrap the whole expression in a const block and that'll be evaluated at compile time. For example:

    let a = const {
        let mut x: u64 = 0;
        let mut k = 5;
        loop {
            if k == 0 {
                break x;
            }
            k -= 1;
            x += 2;
            x *= x;
        }
    };
Re:Crabtime: Zig's Comptime in Rust by LegionMammal978 on Saturday March 22, 2025 @07:18PM (#43449481)
Yeah, it's nice that const blocks are stable now. Before them, you had to use hacks like defining a trait impl with an associated const, which was verbose and messy.

(If I recall correctly, one of the big questions was "Will const blocks unreachable at runtime still be evaluated at compile time?" It looks like the answer was to leave it unspecified.)