我之前遇到了一个奇怪的情况。我写了:
if rand::random() < self.food_chance {...}
(self.food_chance
是 f32
类型)
我遇到了类型推断错误:
|
71 | if rand::random() < self.food_chance {
| ^^^^^^^^^^^^ cannot infer type for `T`
然而,这段代码可以编译。
if self.food_chance > rand::random() {...}
我有疑问,这种行为有原因吗?难道这是期望的行为吗?
我对类型推断理论有一些了解,大多数算法对左侧/右侧都没有偏见,所以我倾向于认为这是预期的行为而不是明显的错误。
我尝试过搜索相关信息,但从未找到过类似的结果。