Summary
Rust's standard library includes several implementations for basic things such as sorting or searching on its primitive slice type and the Iterator<T>
trait. The slice type in particular has many highly important functions to offer.
binary_search()
is a generic implementation of the binary search concepts provided on the slice type. Vec<T>
can be quickly and easily (and implicitly) converted into a slice, making this a universally available function. However, it requires a sorting order to be present in the slice to work (and it won't fail if it's not) and, if custom types are used, an implementation of the Ord
trait.
In case the slice cannot be sorted beforehand, the Iterator<T>
variable's implementation of position()
(of find()
) provides a basic linear search that returns the first position of the element.
Sorting is provided in a generic function, but comes in two flavors: stable and unstable. The regular sort()
function uses a merge sort variation called Timsort...