Skip to content

Tracking issue for Iterator::partition_in_place #62543

Open

Description

@cuviper

View all comments

/// Reorder the elements of this iterator *in-place* according to the given predicate,
/// such that all those that return `true` precede all those that return `false`.
/// Returns the number of `true` elements found.
fn partition_in_place<'a, T: 'a, P>(mut self, ref mut predicate: P) -> usize
where
    Self: Sized + DoubleEndedIterator<Item = &'a mut T>,
    P: FnMut(&T) -> bool,

feature = "iter_partition_in_place"

ref: #62278

Activity

  1. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Jul 9, 2019
  2. PurpleMyst commented on May 25, 2020

    @PurpleMyst

    What's blocking this from stabilization?

  3. cuviper commented on May 26, 2020

    @cuviper
    MemberAuthor

    There's no blocker as far as I know. It would be nice to see some real use for experience, e.g. in the compiler or standard library itself, but I don't think that's required.

  4. PurpleMyst commented on May 26, 2020

    @PurpleMyst

    I've rewritten exactly this function before to avoid depending on nightly features. Is there any way we can put this up for stabilization?

  5. cuviper commented on Jun 2, 2020

    @cuviper
    MemberAuthor

    There's a documented process for stabilizing language features, but I'm not aware of anything special for libraries. AFAIK, you can just open a pull request with the change, and the libs team will put the proposal through FCP. For example, #71886 stabilized a couple numeric methods.

  6. added
    Libs-SmallLibs issues that are considered "small" or self-contained
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Jul 29, 2020
  7. ogoffart commented on Sep 9, 2020

    @ogoffart
    Contributor

    Maybe a stupid question, but why is the method in the Iterator trait, and not in the DoubleEndedIterator trait?

  8. cuviper commented on Sep 9, 2020

    @cuviper
    MemberAuthor

    why is the method in the Iterator trait, and not in the DoubleEndedIterator trait?

    It's a reasonable question. There's a little prior art in Iterator::rev and rposition, and the latter is especially weird given that rfind is on DoubleEndedIterator.

    I guess the current DoubleEndedIterator methods are exclusively about traversing backwards. With partition_in_place, it's reading both ends, but the traversal order is not the main point of the method.

    But that's pretty loose justification, and I think it would also be reasonable to move it. 🤷

  9. scottmcm commented on Sep 10, 2020

    @scottmcm
    Member

    Naming thought: should this be partition_unstable_in_place, following the sort vs sort_unstable distinction?

  10. cuviper commented on Sep 10, 2020

    @cuviper
    MemberAuthor

    IMO, "unstable" is only useful if we think there might be a stable variant, but you would need a side buffer of displaced items to manage that. The documentation does say that relative order is not maintained.

  11. 10 remaining items

  12. clarfonthey commented on Apr 16, 2022

    @clarfonthey
    Contributor

    I commented on #54279 about the weirdness of not finding this as an inherent method on slice; while after seeing the algorithm explained in the docs, it makes sense to me why this doesn't have to be a slice-specific method, it's not at all obvious before that explanation.

    I understand that there are theoretical data structures that would not be represented internally as slices for which this could be implemented, but would said data structures expose a mutable iterator? Like, applying this to a binary tree would destroy the interior structure of the tree, for example, and it seems like this would only be done if you're destroying the structure and converting it to something else anyway.

    Like, it seems like this would be useful as a slice method simply for the ability to combine it with a split_at_mut instead of returning an index, so, maybe that is enough to warrant just moving it to a slice-inherent method and maybe adding the iterator version if it's seen as useful later.

  13. cuviper commented on Apr 17, 2022

    @cuviper
    MemberAuthor

    LinkedList and VecDeque could plausibly want this via iter_mut() as well.

  14. clarfonthey commented on Apr 17, 2022

    @clarfonthey
    Contributor

    ...right, I dunno why I forgot the obvious ones.

    Still, beyond those, I wonder whether it's that worth it to expose on Iterator instead of just offering the algorithm on those collections directly. And in the case of VecDeque, a lot of operations will just make_contiguous first anyway.

  15. orlp commented on Dec 4, 2023

    @orlp
    Contributor

    I think this function is misguided on Iterator and should be part of [T].

  16. AntoinePrv commented on Nov 5, 2024

    @AntoinePrv

    I'd like to see this applicable to iterators and not only on specific data structure. There are many data structures out there that would exposed a mutable iterator but cannot be a slice. I'm thinking of scientific libraries: a strided iterator on a matrix, a chunked array in Apache Arrow / Polars, ...

  17. markg85 commented on Sep 30, 2025

    @markg85

    Hi,

    I was just referred to this function by an LLM. Funny :)

    Just a very small question/remark.
    In sorting algorithms where equal elements are not guaranteed to maintain order the function is often suffixed by _unstable. And when order is preserved there is no additional suffix.

    This function in it's docs currently has the mention: The relative order of partitioned items is not maintained.

    Hence my question: Is there a reason why this is not suffixed with _unstable?

  18. cuviper commented on Sep 30, 2025

    @cuviper
    MemberAuthor

    If you expand GitHub's hidden comments, you can see that "unstable" has come up before. My take remains that it only makes sense to name it that way if we think we might also offer a stable variant, and I don't think we would.

    However, select_nth_unstable doesn't have a stable variant either, so... 🤷

  19. markg85 commented on Sep 30, 2025

    @markg85

    If you expand GitHub's hidden comments, you can see that "unstable" has come up before. My take remains that it only makes sense to name it that way if we think we might also offer a stable variant, and I don't think we would.

    Sorry for that, i should've looked!

    However, select_nth_unstable doesn't have a stable variant either, so... 🤷

    That's not really a scalable approach, is it? I don't disagree with your thinking but i also wouldn't do that. The reason i say that is for future new functions. If you don't suffix it now then a future version will have to be suffixed (or be a v2 or something). And you get naming inconsistencies where one algorithm is stable without a suffix and the other is unstable without a suffix.

    But there's probably something to be said for either argument.

  20. clarfonthey commented on Sep 30, 2025

    @clarfonthey
    Contributor

    After encountering a potential use for this function, my opinion is… honestly, I still think it should be implemented directly on slices instead.

    In particular, I think it should work closer to how retain does, minus actually dropping anything. Maybe even a version that works like retain_mut. In particular, the fact that those both retain stability of the items on at least one side of the partition is desirable, IMHO. Whereas this version basically can never be stable without some kind of trickery.

    The other benefit of implementing directly on slices is it can perform a split_at for you and directly return two slices, which seems kind of nice.

  21. LegionMammal978 commented on Jan 17, 2026

    @LegionMammal978
    Contributor

    Regarding the FIXME comment w.r.t. the count overflowing: we have precedent for this in Iterator::count() and Iterator::position(), which document that they either panic or produce a wrong answer depending on whether overflow checks are enabled. We could simply add a similar notice to Iterator::partition_in_place(), and also note for clarity that if the increment, predicate, or iterator operations panic, then all elements remain, but the resulting order is unspecified.

    (Of course, this is moot if we specialize this function to slices and other containers instead of generic &mut T iterators.)

    Also, this function could definitely have better visibility. I just went looking for this for a Gaussian-elimination routine, and I had lots of trouble finding it. One alternative: add it as a generic Iterator method (or free function), and also add it as an inherent slice method. There is some precedent for this, with <[T]>::is_sorted_by_key() simply calling out to Iterator::is_sorted_by_key().

  22. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
  23. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    and removed
    Libs-SmallLibs issues that are considered "small" or self-contained
    on Oct 7, 2026
  24. removed
    I-libs-radarLibs issues that are tracked on the team's radar.
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions