gpt4 book ai didi

rust - 如何匹配 Rust 中的标志/位掩码?

转载 作者:行者123 更新时间:2023-11-29 07:49:30 24 4
gpt4 key购买 nike

对位标志进行典型检查:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com