I was about to post here about how I found the perspective baffling that Result
should be only for errors, or that binary_search
should return anything other than Result<usize,usize>
, citing the plethora of useful methods you lose access to when you write some one-off type like enum BinarySearchResult
.
But then I recalled something: The one time I used binary_search
myself, I didn’t get to use any of those cool methods, because what I really needed was this:
fn insertion_index(vec: Vec<T>, x: &T) -> usize {
match vec.binary_search(x) {
Ok(i) -> i,
Err(i) -> i,
}
}
which is a functionality Result
doesn’t have a method for, perhaps due to the common perception that Result<T,T>
is absurd.
(that said, I would sooner expect to see -> (bool, usize)
than -> ::std::slice::SomeEnumThatIsntInPrelude
)