Macro std::compile_fail
Causes the compilation to fail if reached.
This is useful when conditional compilation is used with #[cfg(...)]
attributes, but it can also be used
in generic context (e.g. with when
expressions).
Can accept format arguments, which are formatted using the same syntax as std::fmt::format_args!
. For obvious reasons,
they must be const-evaluable.
Examples
fn fork() -> bool {
#[cfg(target_os="windows")]
compile_fail!("fork() is not supported on Windows");
libc::fork() == 0
}
let is_child = fork();
if is_child {
println!("Hello from child");
} else {
println!("Hello from parent");
}
Run this example
fn type_of_value<T>(v: T) -> &[u8] {
when v is i32 {
"i32"
} else when v is &[u8] {
"&[u8]"
} else {
compile_fail!("unsupported type")
}
}
println!("{}", type_of_value(1)); // "i32"
println!("{}", type_of_value("hello world")); // "&[u8]"
// println!("{}", type_of_value(true)); // Would fail to compile
Run this example