对位标志进行典型检查:
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(),
_ => {},
}
关于rust - 如何匹配 Rust 中的标志/位掩码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39178648/