Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Rust disallow "let v = Vec<i32>::new();"? [closed]

Tags:

generics

rust

I feel Vec::<i32>::new() is not consistent with 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
like image 976
txhwind Avatar asked Nov 19 '25 04:11

txhwind


1 Answers

The notation is affectionately called the turbofish.

Vec<i32>::new() presents a trade-off when parsing. Is the < introducing the generic argument, or is it a less than operator? To tell the difference requires potentially unbounded look-ahead. The turbofish avoids that.

That is not universally viewed as important. It has been proposed to allow Vec<i32>::new(), but the proposal was abandoned. See here.

That said, specifying the type parameter is less commonly needed than in comparable languages due to type inference as you noted in your second line.

like image 100
Jeff Garrett Avatar answered Nov 20 '25 18:11

Jeff Garrett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!