Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading Iterator::filter_map example #46734

Closed
dtolnay opened this issue Dec 14, 2017 · 2 comments
Closed

Misleading Iterator::filter_map example #46734

dtolnay opened this issue Dec 14, 2017 · 2 comments
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools

Comments

@dtolnay
Copy link
Member

dtolnay commented Dec 14, 2017

This is the example currently given for Iterator::filter_map:

let a = ["1", "2", "lol"];

let mut iter = a.iter().filter_map(|s| s.parse().ok());

assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);

I find it a bit misleading that there are 3 array elements and 3 asserts that do not correspond in the obvious way. It is too easy to skim this and interpret it as:

let a = ["1", "2", "lol"];

let mut iter = a.iter().map(|s| s.parse().ok());

assert_eq!(iter.next().unwrap(), Some(1));
assert_eq!(iter.next().unwrap(), Some(2));
assert_eq!(iter.next().unwrap(), None);

I think using ["1", "lol", "3", "NaN", "5"] with four asserts would avoid the confusion.

@dtolnay dtolnay added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 14, 2017
@dtolnay
Copy link
Member Author

dtolnay commented Dec 14, 2017

Ping @tshepang who last worked on this example. Would you be interested in making this change?

@tshepang
Copy link
Member

will do

steveklabnik added a commit to steveklabnik/rust that referenced this issue Dec 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools
Projects
None yet
Development

No branches or pull requests

2 participants