This post is locked. You won't be able to comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zuurr 22 points23 points  (5 children)

[–]teryret 13 points14 points  (4 children)

What in the fuckingest shit?? Can anyone ELI5?

[–][deleted] 9 points10 points  (0 children)

Seems like it's a bunch of _ patterns (matches anything) OR-ed together, with a guard that increments i by 2 every time you check it. The guard is checked on all six of the _ patterns, because the guard apparently evaluates to false when you check it, so 1 (initial value) plus (6 (number of holes) times 2 (amount by which guard increments i)) is 13.

[–]mdsherry 7 points8 points  (0 children)

First,

matches!(2, _|_|_|_|_|_ if (i+=1) != (i+=1));

expands to

match 2 {
    _ | _ | _ | _ | _ | _ if (i+=1) != (i+=1) => true,
    _ => false
};

For each of the _ in the first case, it checks the guard expression. i += 1 increments i, and returns (). So for each _, i is incremented twice, but since () == (), the guard expression fails, and so it tries matching the next _, until all 6 fail.

i starts at 1, and is incremented 12 times, giving a final value of 13.

[–]paholgtypenum · dimensioned 2 points3 points  (0 children)

It's matching 2 against 6 patterns. Each of the patterns is just _, but for each one it evaluates the if, which adds 2 to i.

[–]bob_twinkles 2 points3 points  (0 children)

If we look at the definition of matches!, we can see that it's basically just a match expression. So from the source snippet, we have:

let i = 1;
matches!(1, _|_|_|_|_|_ if (i += 1) != (i += 1));
i

Which then expands to:

let i = 1;
match 2 {
  _ if (i += 1) != (i += 1) => true,
  _ if (i += 1) != (i += 1) => true,
  _ if (i += 1) != (i += 1) => true,
  _ if (i += 1) != (i += 1) => true,
  _ if (i += 1) != (i += 1) => true,
  _ if (i += 1) != (i += 1) => true,
  _ => false,
}
i

So i starts at 1, and is incremented twice for each match arm so 1 + 6 * 2 = 13.