Take a typical check for bit-flags:
if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
How would this be written as a match
statement?
Take a typical check for bit-flags:
if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
How would this be written as a match
statement?
if (v & (1 << 0)) != 0 { foo(); }
else if (v & (1 << 1)) != 0 { bar(); }
else if (v & (1 << 2)) != 0 { baz(); }
Such concrete code can be rewritten like this:
match v.trailing_zeros() {
0 => foo(),
1 => bar(),
2 => baz(),
_ => {},
}
As fghj answered, it is possible to do this:
match v.trailing_zeros() {
0 => foo(),
1 => bar(),
2 => baz(),
_ => {},
}
This would be preferable to using if
/else
, not only for readability, as rustc generates less assembly for the match statement than for the if
/else
clause: 23 lines of logic vs 9 (see Compiler explorer). It's possible that this could differ depending on architecture, however, particularly for RISC processors
Benchmarking both methods with 8 bits, they have exactly the same performance:
running 2 tests
test bench_if_else ... bench: 62 ns/iter (+/- 0)
test bench_match ... bench: 61 ns/iter (+/- 2)
© 2022 - 2024 — McMap. All rights reserved.
if v & (1 << 0) > 0
etc.? – Astartematch
when you are testing single bits. – Pindus