<分区>
我觉得Vec::<i32>::new()
与Vec<i32>
不一致
let v = Vec<i32>::new(); // cannot compile
let v1 : Vec<i32> = Vec::new(); // auto inference type parameters
let v2 = Vec::<i32>::new(); // quite weird
<分区>
我觉得Vec::<i32>::new()
与Vec<i32>
不一致
let v = Vec<i32>::new(); // cannot compile
let v1 : Vec<i32> = Vec::new(); // auto inference type parameters
let v2 = Vec::<i32>::new(); // quite weird
最佳答案
该符号被亲切地称为涡轮鱼。
Vec<i32>::new()
在解析时呈现一个权衡。是 <
引入通用参数,还是小于运算符?分辨差异需要潜在的无限前瞻。涡轮鱼避免了这种情况。
这并不是普遍认为重要的。已经提议允许Vec<i32>::new()
, 但该提议被放弃了。参见 here .
也就是说,由于您在第二行中提到的类型推断,与可比语言相比,指定类型参数的需求较少。
关于generics - 为什么 Rust 不允许 "let v = Vec<i32>::new();"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69848709/