I was trying to write the following code:
trait Foo {
type Output;
fn bar() -> Self::Output;
fn baz() -> Self::Output {
const match Self::Output { // imaginary syntax
() => (), // compile this statement only if Output == Unit
_ => bar() // Otherwise compile this statement
}
}
}
Since Output
is a generic type, I can't instantiate a value (and it could be expensive to do so). In C++, I would have just used if constexpr(std::same<Output, void>::value)
or created a template function and specialized it for void
, but I didn't find a way to do it in Rust.