DanielKeep / tlborm

The Little Book of Rust Macros
880 stars 96 forks source link

Logarithmic counting #17

Open tobia opened 7 years ago

tobia commented 7 years ago

Here's an efficient counting macro I came up with while reading the chapter on Counting.

It can count a practically unlimited number of tokens with Rust's default recursion limit, it's faster than Slice length, and it produces a constant number.

I assert_eq'ed it on all numbers from 0 to 50, just to be on the safe side.

Enjoy!

RReverser commented 6 years ago

Wow, this is great idea!