Do you think drop order should be part of a function’s API contract?

70 points Natsuawa_Keiko 22 comments

Recently, I wrote a toy crate similar to ⁠`smallvec⁠`. When I was implementing ⁠`nth_back⁠` for my iterator, I realized that the default implementation continuously calls ⁠`next_back⁠` and drops items in reverse order (which is intuitive), whereas for performance reasons, I implemented it with ⁠`ptr::drop_in_place⁠`, which drops items in forward order. In a language where the drop guard pattern is so prevalent, I think this is not a trivial thing. What's your opinion?

P.S. `std::vec::IntoIter⁠::nth_back` drops items in forward order too, if you are curious.