Returning a Result from slice's binary_search is weird

What's the reasoning behind that choice? Not having a value in a slice could hardly be called an error -- that would really depends on the application's semantics. With the most recent version of clippy I now get a lint warning about ignoring the value returned in the Err variant, even though that's perfectly fine for my purposes. I'm not ignoring an error, after all, I'm just reacting to a value not being stored in a slice, in which case I don't care where it would have been, had it been inserted before.
If there another variant of binary_search that uses a more appropriate return type?

You can convert to an Option quite easily using ok().

I agree that conceptually it’s not an error. Introducing a dedicated type is extra maintenance, documentation, testing, etc burden, especially given that you can go from Result to Option easily via existing adapter methods.

While they don't exist today, there's discussion of variants of binary_search with different APIs at

https://github.com/rust-lang/rfcs/issues/2184

1 Like