让我们以位标志的典型检查为例:
if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
这句话如何用
match
语句来写?if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
这样的具体代码可以重写为:
match v.trailing_zeros() {
0 => foo(),
1 => bar(),
2 => baz(),
_ => {},
}
if v & (1 << 0) > 0
等等? - Dogbertmatch
没有意义。 - starblue