Skip to content

Commit

Permalink
Use simpler code example for E0631 long error.
Browse files Browse the repository at this point in the history
  • Loading branch information
reese committed Dec 3, 2019
1 parent 7693bb9 commit 26a1ba8
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/librustc_error_codes/error_codes/E0631.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ This error indicates a type mismatch in closure arguments.
Erroneous code example:

```compile_fail,E0631
fn test_strings(string_vec: Vec<String>) -> Vec<bool> {
string_vec
.iter()
.map(|arg: &i32| arg.eq("Test String"))
.collect()
fn foo<F: Fn(i32)>(f: F) {
}
fn main() {
foo(|x: &str| {});
}
```

The closure passed to `map` expects a `&String` argument, since `some_vec`
has the type `Vec<String>`.
However, the closure argument is annotated as an `&i32`, which does not match
the type of the iterable.
The error occurs because `foo` accepts a closure that takes an `i32` argument,
but in `main`, it is passed a closure with a `&str` argument.

This can be resolved by changing the type annotation or removing it entirely
if it can be inferred.

```
fn test_strings(string_vec: Vec<String>) -> Vec<bool> {
string_vec
.iter()
.map(|arg| arg.eq("Test String"))
.collect()
fn foo<F: Fn(i32)>(f: F) {
}
fn main() {
foo(|x: i32| {});
}
```

0 comments on commit 26a1ba8

Please sign in to comment.