Skip to content

Commit

Permalink
Improve examples of E0102
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 7, 2015
1 parent 2a08cff commit 5aa6c15
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,27 +1123,39 @@ You hit this error because the compiler lacks information to
determine a type for this variable. Erroneous code example:
```
fn demo(devil: fn () -> !) {
let x: &_ = devil();
// error: cannot determine a type for this local variable
}
fn oh_no() -> ! { panic!("the devil is in the details") }
fn main() {
let x: &_; // error: cannot determine a type for this local variable
demo(oh_no);
}
```
You have two possibilities to solve this situation:
* Give an explicit definition of the variable
* Infer the variable
To solve this situation, constrain the type of the variable.
Examples:
```
fn some_func(x: u32) {
fn some_func(x: &u32) {
// some code
}
fn main() {
let x = 0u32; // ok!
// or:
let x = 0;
fn demo(devil: fn () -> !) {
let x: &u32 = devil();
// Here we defined the type at the variable creation
let x: &_ = devil();
some_func(x);
// Here, the type is determined by the function argument type
}
fn oh_no() -> ! { panic!("the devil is in the details") }
fn main() {
demo(oh_no);
}
```
"##,
Expand Down

0 comments on commit 5aa6c15

Please sign in to comment.