ZST const assertion
⚓ Rust 📅 2025-10-21 👤 surdeus 👁️ 3I'm currently trying to implement in a struct a const assertion to check that the struct is not a ZST.
My current implementation is:
struct Foo;
impl Foo {
pub const _VALIDITY: () = assert!(size_of::<Self>() != 0, "Foo cannot be a ZST");
}
but this doesn't actually throw a compile-time error. I noticed that the code throws an error only if I use this constant in a function, just like this:
struct Foo;
impl Foo {
pub const _VALIDITY: () = assert!(size_of::<Self>() != 0, "Foo cannot be a ZST");
fn _bar() {
Self::_VALIDITY;
}
}
so, I wanted to ask
- Is this the correct way to implement this check?
- If so, is the function that uses this constant necessary, or am I doing something wrong?
3 posts - 3 participants
🏷️ Rust_feed