Idiomatic Rust? Implementing binary search (part 2)

submited by
Style Pass
2021-06-12 23:30:05

This is a follow-up to the original article where I looked at a few ways to improve my Rust implementation of Binary search - with a focus on removing 'mistakes' and making it as 'idiomatic' as possible.

The most common piece of feedback I received on the first article was related to the comparison between the middle value and high/low cursors.

In my original Tweet (even before the first article) I had presented a variant of the algorithm that used pattern matching for this comparison via Rust's match construct.

If we apply the feedback from the first article to this implementation, we'd end up with the following - which is just a bit cleaner since it removes some casting along with the manual 'flooring' I was originally doing.

This is where it became a bit complicated. I originally received some feedback on Twitter that suggested because I'm not using the return value of the match block in any of the arms, along with the fact that I'm only using it to mutate the high/low cursors, that this could be deemed not idiomatic Rust...

Leave a Comment