这个问题与这里提出的问题有关:如何声明一个变量但不分配它?
我的问题略有不同。MRE:
let var: MyGenericType;
for value in vec_of_values_of_my_generic_type {
// one of the values is mathematically guaranteed to meet this condition because the vector is special
if <value meets a certain condition> {
var = value;
}
}
<use var>
错误:
error[E0381]: used binding `var` is possibly-uninitialized
--> example/src/lib.rs:127:23
|
120 | let var: MyGenericType;
| -- binding declared here but left uninitialized
...
123 | var = value;
| -- binding initialized here in some conditions
...
127 | foo(var);
| ^^ `var` used here but it is possibly-uninitialized
我该如何修复这个问题?