5

The match in Rust only executes one arm. I found this code snippet from Murmurhash 3:

switch(len & 15)
{
  case 15: k2 ^= ((uint64_t)tail[14]) << 48;
  case 14: k2 ^= ((uint64_t)tail[13]) << 40;
  case 13: k2 ^= ((uint64_t)tail[12]) << 32;
  case 12: k2 ^= ((uint64_t)tail[11]) << 24;
  case 11: k2 ^= ((uint64_t)tail[10]) << 16;
  case 10: k2 ^= ((uint64_t)tail[ 9]) << 8;
  case  9: k2 ^= ((uint64_t)tail[ 8]) << 0;
           k2 *= c2; k2  = ROTL64(k2,33); k2 *= c1; h2 ^= k2;

  case  8: k1 ^= ((uint64_t)tail[ 7]) << 56;
  case  7: k1 ^= ((uint64_t)tail[ 6]) << 48;
  case  6: k1 ^= ((uint64_t)tail[ 5]) << 40;
  case  5: k1 ^= ((uint64_t)tail[ 4]) << 32;
  case  4: k1 ^= ((uint64_t)tail[ 3]) << 24;
  case  3: k1 ^= ((uint64_t)tail[ 2]) << 16;
  case  2: k1 ^= ((uint64_t)tail[ 1]) << 8;
  case  1: k1 ^= ((uint64_t)tail[ 0]) << 0;
           k1 *= c1; k1  = ROTL64(k1,31); k1 *= c2; h1 ^= k1;
};

This switch is the traditional C switch. Is it possible to write this in Rust in an elegant way? I couldn't find a way to implement this with match.

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
JACK M
  • 2,627
  • 3
  • 25
  • 43
  • 1
    that's called [tag:fall-through]. See [Idiomatic match with fall-through in Rust](https://stackoverflow.com/q/42509526/995714) – phuclv Jun 28 '19 at 06:43
  • 1
    How does the marked duplicate provides an answer to this question ? A playground link please :) – Svetlin Zarev Jun 28 '19 at 16:46
  • I don't think the duplicate actually answers the general case of this question. – wizzwizz4 Nov 17 '19 at 16:07
  • I believe Matthieu's answer to the other question adequately answers this one. That is: fall-through is not supported, so you'll need a loop. – trent Nov 17 '19 at 21:58
  • 1
    If you would like to check how fall-through is implemented in Rust, check this repo https://github.com/yyklll/murmurhash3 – JACK M Nov 19 '19 at 02:11

0 Answers0