4 ms·
fn main() { if false { const _:() = panic!(); } } Fails to compile in Rust.
by dlahoda 1y ago
fn main() {
if false {
const _:() = panic!();
}
}
Fails to compile in Rust.
- Ygg2 1y agoWhy would it? If I recall correctly, const and static stuff basically gets inlined at the beginning of the program.
- Sharlin 1y agoSure, because it's compile-time code inside a (semantically) run-time check. In recent Rust versions you can do fn main() { const { if false { let _:() = panic!(); } } } which compiles as expected. (Note that if the binding were `const` instead of `let`, it'd still have failed to compile, because the semantics don't change.)
- tialaramex 1y agoPerhaps more succinctly: fn main() { const _:() = const { if false { panic!() } }; } It's fine that we want a constant, it's fine that this constant would, when being computed at compile time, panic if false was true, because it is not.
- dlahoda 1y agonot sure it is to be equivalent to zig. in zig they have one brach const. in rust example from you, whole control flow ix is const. which is not rquivalent to zig. so how to have non const branches?
- the__alchemist 1y agoI have no idea what that's trying to do. A demonstration that rust is a large language with different dialects! A terse statement with multiple things I don't understand: - Assigning a const conditionally? - Naming a const _ ? - () as a type? - Assigning a panic to a constant (or variable) ? To me it might as well be: fn main() { match let { if ()::<>unimplemented!() -> else; } }