Skip to content

Clarify behavior of map/filter #30632

Closed
Closed

Description

@Stebalien

Apparently iter::Map and iter::Filter implement DoubleEndedIterator. This could lead to unexpected behavior:

let counter = iter::repeat(()).map({
    let mut ctr = 0;
    move |_| {
        ctr += 1;
        ctr
    }
});

let counter2 = counter.reverse(); // Doesn't reverse anything.

These adapters are only really reversible when passed closure is side-effect free. Unfortunately, we can't simply say that map calls the passed closure on each element yielded because counter.last() != counter.reverse().next().

Activity

  1. steveklabnik commented on Dec 31, 2015

    @steveklabnik
    Contributor

    /cc @rust-lang/libs

  2. Kimundi commented on Jan 1, 2016

    @Kimundi
    Contributor

    I don't think we need to change anything here apart from maybe improving the docs by pointing out this pitfall.

    The behavior of last(), reverse() etc is well specified, so different behavior if combined with side-effecting closures should be too.

  3. added a commit that references this issue on Feb 2, 2016
    ae96e51
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions