Skip to content

This block describes the typing incorrectly. #42

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

Merged
merged 1 commit into from
Jul 30, 2020

Conversation

elfsternberg
Copy link
Contributor

Consider the following demonstration (this code compiles under Rust 2018 as a drop in for the contents of main.rs after cargo new mapdemo --bin, edit main.rs, and then cargo run):

fn main() {
	let x = [1, 2, 3, 4, 5];
	let z = x.iter().map(|&x| format!("{}", x)).collect::<Vec<String>>();
	println!("{:?}", z);
}

This prints an array of strings:

["1", "2", "3", "4", "5"]

The self argument to map will be an Iter<u64>. The Self::Item type is therefore u64. The return type of the function being passed to map is String. This is what the B type variable indicates: the return type of the function, as can be clearly seen in the second line of the where clause in your example:

    F: FnMut(Self::Item) -> B

Consider the following demonstration (this code compiles under Rust 2018
as a drop in for the contents of main.rs after `cargo new mapdemo --bin;
cargo run`):

```
fn main() {
	let x = [1, 2, 3, 4, 5];
	let z = x.iter().map(|&x| format!("{}", x)).collect::<Vec<String>>();
	println!("{:?}", z);
}
```

The `self` argument to map will be an `Iter<u64>`.  The `Self::Item`
type is therefore `u64`.  The return type of the function being passed
to map is `String`.  This is what the `B` type variable indicates: the
return type of the function, as can be clearly seen in the second line
of the `where` clause in your example:

```
    F: FnMut(Self::Item) -> B
```
@Dhghomon
Copy link
Owner

Thank you! I also see that http://hemingwayapp.com/ is happy with the way you phrased it.

@Dhghomon Dhghomon merged commit e09e309 into Dhghomon:master Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants