我早些时候遇到了一个奇怪的情况.我写:
if rand::random() < self.food_chance {...}
Run Code Online (Sandbox Code Playgroud)
(self.food_chance属于类型f32)
我得到了一个类型推理错误:
|
71 | if rand::random() < self.food_chance {
| ^^^^^^^^^^^^ cannot infer type for `T`
Run Code Online (Sandbox Code Playgroud)
但是这段代码编译
if self.food_chance > rand::random() {...}
Run Code Online (Sandbox Code Playgroud)
而且我想知道.这种行为有原因吗?它甚至是预期的行为吗?
我对类型推理理论有一点了解,我知道大多数算法都与lhs/rhs无关,所以我很想把它看成是预期的行为而不是直接的bug.
我试着四处寻找,但我从来没有接近过这个.