Optimizing FizzBuzz in Rust

21 points by nrposner


joshka

If you’re writing a proc macro that expands fizzbuzz, the optimal approach for low numbers of iterations would likely be expanding the result to a string instead of code. E.g. “1\n2\nFizz\n4\nBuzz…”. This would be pretty much zero runtime cost.

I recall being asked in an interview effectively the same question with a small twist - convert time of day to english (e.g. “A quarter to 3”). My immediate answer was there’s 24*60 items which for anything except an embedded context is small enough to pre-compute a lookup table, and which likely has a decent performance for all use cases. The interviewer hadn’t expected an answer that breaks the question’s assumptions, so I suggested that we do the programming bit for how we’d generate that table so that he had an actual data point to measure me against.