Add examples to document the return type of quickselect functions by Takashiidobe · Pull Request #121187 · rust-lang/rust (original) (raw)
Currently, select_nth_unstable
, select_nth_unstable_by
, and select_nth_unstable_by_key
's examples do not show how to use the return values of the functions in an example, so this PR adds that in.
Note: I didn't know what to call the parameters, so I settled on lesser, median, greater because the example is used for median finding so I retained that naming for the pivot, but lesser and greater are poor names for the example that sorts in descending order, because lesser and greater are then flipped.
I think it's common to say "lo" and "hi" for low and high respectively, but that's also not great when the comparator flips the elements. Otherwise, "left" and "right" are also commonly used but I think that's poor naming because some languages read right to left so those names are also unintuitive.
Lesser and greater are also not that great but I found a test that used less
, equal
, greater
so I took that:
| v.select_nth_unstable_by(pivot, |_, _| *[Less, Equal, Greater].choose(&mut rng).unwrap()); | | --------------------------------------------------------------------------------------------------- |