To add to the potential churn, there are also nth_back
in DoubleEndedIterator
(nth_back(0) == last()
), as well as slice::select_nth_unstable
and its variants.
Some thoughts on why I don't see the naming as a problem:
Method naming uses the natural language meanings, where ordinals start from "first". That is, "first" is an English word for the element before all others in a sequence just like "last" is a word for the one after all others. Adding in "zeroth" as a possibility can only add confusion.
If I see a method named "forty_second", I expect it to return the element at index 41, because in zero-based indexing that is the forty-second element. Similarly, in "nth", the name invokes reference to the ordinal numbers ("first", "second", ...), but as a crucial difference the parameter is taken as a number, where it would be quite surprising to not use zero-based indexing.
One way I can understand nth
is that it is mapping the parameter value of type usize to the ordinals, and the zero-based indexing is part of that mapping. In pseudo-code:
fn nth(&mut self, n: usize) -> T {
const ORDINALS: [fn(&mut Self) -> T] = [first, second, third, ...];
(ORDINALS[n])(self)
}
To clarify, the only issue I see here is that nth(4)
looks like it could mean fourth()
, but one already has to read v[4]
as the fifth element.