Skip to content

Commit 5aa6c15

Browse files
Improve examples of E0102
1 parent 2a08cff commit 5aa6c15

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,27 +1123,39 @@ You hit this error because the compiler lacks information to
11231123
determine a type for this variable. Erroneous code example:
11241124
11251125
```
1126+
fn demo(devil: fn () -> !) {
1127+
let x: &_ = devil();
1128+
// error: cannot determine a type for this local variable
1129+
}
1130+
1131+
fn oh_no() -> ! { panic!("the devil is in the details") }
1132+
11261133
fn main() {
1127-
let x: &_; // error: cannot determine a type for this local variable
1134+
demo(oh_no);
11281135
}
11291136
```
11301137
1131-
You have two possibilities to solve this situation:
1132-
* Give an explicit definition of the variable
1133-
* Infer the variable
1134-
1138+
To solve this situation, constrain the type of the variable.
11351139
Examples:
11361140
11371141
```
1138-
fn some_func(x: u32) {
1142+
fn some_func(x: &u32) {
11391143
// some code
11401144
}
11411145
1142-
fn main() {
1143-
let x = 0u32; // ok!
1144-
// or:
1145-
let x = 0;
1146+
fn demo(devil: fn () -> !) {
1147+
let x: &u32 = devil();
1148+
// Here we defined the type at the variable creation
1149+
1150+
let x: &_ = devil();
11461151
some_func(x);
1152+
// Here, the type is determined by the function argument type
1153+
}
1154+
1155+
fn oh_no() -> ! { panic!("the devil is in the details") }
1156+
1157+
fn main() {
1158+
demo(oh_no);
11471159
}
11481160
```
11491161
"##,

0 commit comments

Comments
 (0)