我有一个有趣的案例,我不太明白。
我尝试在迭代中不断打开一些代码。整个迭代过程中条件都是相同的,因此我没有使用常规的 if else,而是尝试使用 const 泛型布尔值来执行此操作。
一个简单的例子是这样的:
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
use const_assert::{Assert, IsTrue};
enum XorY {
X,
Y
}
fn foo(count: usize, do_special: Option<XorY>) {
match do_special {
Some(XorY::X) => foo_inner::<true, false>(count),
Some(XorY::Y) => foo_inner::<false, true>(count),
_ => foo_inner::<false, false>(count),
}
}
const fn constraint(x: bool, y: bool) -> bool {
!(x && y)
}
fn foo_inner<const X: bool, const Y: bool>(count: usize)
where Assert<{constraint(X, Y)}> : IsTrue
{
for i in 0..(count - 1)
{
foo_iter::<false, Y>(i);
}
foo_iter::<X, Y>(i);
}
fn foo_iter<const X: bool, const Y: bool>(index: usize)
: where Assert<{constraint(X, Y)}> : IsTrue
{
if X {
// do x stuff with index
}
else if Y {
// do y stuff with index
}
else {
// do default stuff with index
}
}
但我收到“不受约束的通用常量”错误。
error: unconstrained generic constant
|
| foo_iter::<false, Y>(
| ^^^^^
|
note: required by a bound in `foo_iter`
|
| fn foo_iter<const X: bool, const Y: bool>(
| -------- required by a bound in this function
...
| where Assert<{ constraint(X, Y) }> : IsTrue
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `foo_iter`
help: try adding a `where` bound
|
| where Assert<{ constraint(X, Y) }> : IsTrue, [(); { constraint(X, Y) } as usize]:
|
我不明白为什么 false 不受约束。另外,该建议不起作用。