此代码完美运行(操场):
struct MyStruct<const B: bool>;
impl MyStruct<false> {
pub fn bar() {
println!("false");
}
}
impl MyStruct<true> {
pub fn bar() {
println!("true");
}
}
impl MyStruct<false> {
pub fn foo() {
MyStruct::<false>::bar()
}
}
impl MyStruct<true> {
pub fn foo() {
MyStruct::<true>::bar()
}
}
fn main() {
MyStruct::<false>::foo();
MyStruct::<true>::foo();
}
Run Code Online (Sandbox Code Playgroud)
结果是:
false
true
Run Code Online (Sandbox Code Playgroud)
另一方面,此代码将失败(playground):
struct MyStruct<const B: bool>;
impl MyStruct<false> {
pub fn bar() {
println!("false");
}
}
impl MyStruct<true> {
pub fn bar() { …Run Code Online (Sandbox Code Playgroud)