Rust Reference ยท Operators and Expressions
The ::<> turbofish
Reference for the Rust turbofish ::<>: giving generic arguments to functions, methods and paths in expressions, with parse, collect, sum and const generics.
The turbofish is the syntax ::<...> used in expressions to supply generic arguments explicitly: "5".parse::<u8>(), Vec::<i32>::new(), iter.collect::<Vec<_>>(). It exists because in an expression context a < b > c already means something (two comparisons), so the parser needs the extra :: to know that < starts a list of type arguments.
You only need it when the compiler cannot infer the type from the surroundings. In type positions (annotations, struct fields, signatures) you write plain angle brackets: let v: Vec<i32>. Anywhere inference works, leave the turbofish out.
let n = "42".parse::<i32>().unwrap(); let words = text.split(' ').collect::<Vec<_>>(); let size = size_of::<u64>();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
path::<Type, ...>(args) // free function or associated function Type::<Args>::function(args) // generic type, then function expr.method::<Type, ...>(args) // method with its own generics f::<'static, T, 3>() // lifetimes, types and const arguments
| Part | Description |
|---|---|
| Type argument | A concrete type, or _ to let the compiler infer it. |
| Const argument | A literal, a const item, or a braced expression: f::<{ N + 1 }>(). |
| Lifetime argument | Usually omitted; if given, it goes first. |
Edge cases: the arguments are given in declaration order, and (apart from _) you must supply all of them. A function with impl Trait in argument position can have its explicit generics given, but not the impl Trait ones.
Usage notes
- Why
::: infoo<i32>(x)the parser readsfoo < i32 > (x). The turbofish removes the ambiguity. In types there is no such conflict, soVec<i32>needs no::. - Alternative: annotate the binding.
let n: i32 = s.parse().unwrap();is equivalent to the turbofish version. Choose the turbofish inside a longer method chain, where there is no binding to annotate. collect::<Vec<_>>()is the single most common use: fix the container and let Rust infer the elements.collect::<Result<Vec<_>, _>>()turns an iterator ofResults into aResultof aVec.sumandproductneed it when the element type does not decide the result (for example summing&i32items into ani64).- Generic method on a generic type: both can take arguments:
Vec::<u8>::with_capacity(8)for the type,.iter().map::<String, _>(...)for the method (rarely needed). - Enum variants and structs accept it too:
None::<i32>,Ok::<_, String>(5). The second form is handy for closures and?inmain. - Error to look for: E0282/E0283 "type annotations needed" is the compiler asking for exactly this.
- It costs nothing. Generic arguments are resolved at compile time; the turbofish only guides inference.
Related
- . member access
- Closure syntax
- type and impl
- Tutorial: Generics, Const generics, Iterators