Re:Crabtime: Zig's Comptime in Rust
by throwawaymaths on Saturday March 22, 2025 @06:38PM
(#43449216)
i didn't use reflection in this example, but note that consteval shouldn't be able to do this because I mutate the string; it's not const at comptime.
[ Parent
| on HN ]
I tried your example and got an error:
error: type capture contains reference to comptime var
I'm not sure how you were suppossed to use it but here's my attempt: fn capitalize(tag: []u8) []u8 {
tag[0] &= 95;
return tag;
}
fn MyType(T: type, comptime tag: []u8, comptime count: usize) type {
const capitalized = capitalize(tag);
return struct {
fn name() []const u8 {
return capitalized;
}
array: [count]T,
};
}
pub fn main() void {
comptime var hello: [5]u8 = undefined;
@memcpy(&hello, "hello");
var v: MyType(i32, &hello, 10) = undefined;
v.name();
}
If you allow `capitalized` to be it's own instance then there's no reason to mutate the comptime parameter in the first place, and it can be replicated in C++17.