Skip to content

Using Zip & Take allows accessing the middle a Map iterator without “getting there first” from either side. #85969

Closed
@steffahn

Description

@steffahn

Look at this iterator.

let iterator = (0..100).map(|x| println!("item {}", x));

We all know that when map’s resulting Iterator implements DoubleEndedIterator, then the side-effects can be re-ordered. However, judging by the implementation of map one would probably at least expect that it’s impossible to hit the "item 50" without first either going through at least 0, 1, 2, … 49 or 100, 99, 98, … 51 first. Right?

Well, here we go!

let mut y = iterator.take(51).zip(0..100);
y.next_back();

on beta

item 50

on stable 1.52.0

item 99
item 98
item 97
item 96
item 95
item 94
item 93
item 92
item 91
item 90
item 89
item 88
item 87
item 86
item 85
item 84
item 83
item 82
item 81
item 80
item 79
item 78
item 77
item 76
item 75
item 74
item 73
item 72
item 71
item 70
item 69
item 68
item 67
item 66
item 65
item 64
item 63
item 62
item 61
item 60
item 59
item 58
item 57
item 56
item 55
item 54
item 53
item 52
item 51
item 50

(playground)

@rustbot label T-libs-impl, A-iterators, A-specialization, regression-from-stable-to-beta

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsArea: IteratorsA-specializationArea: Trait impl specializationC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions