File tree Expand file tree Collapse file tree 1 file changed +22
-10
lines changed Expand file tree Collapse file tree 1 file changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -1123,27 +1123,39 @@ You hit this error because the compiler lacks information to
1123
1123
determine a type for this variable. Erroneous code example:
1124
1124
1125
1125
```
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
+
1126
1133
fn main() {
1127
- let x: &_; // error: cannot determine a type for this local variable
1134
+ demo(oh_no);
1128
1135
}
1129
1136
```
1130
1137
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.
1135
1139
Examples:
1136
1140
1137
1141
```
1138
- fn some_func(x: u32) {
1142
+ fn some_func(x: & u32) {
1139
1143
// some code
1140
1144
}
1141
1145
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();
1146
1151
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);
1147
1159
}
1148
1160
```
1149
1161
"## ,
You can’t perform that action at this time.
0 commit comments