Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let n = "42".parse::<i32>().unwrap();
let words = text.split(' ').collect::<Vec<_>>();
let size = size_of::<u64>();

Try it

Rust Where inference needs help

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.

Rust Your own generic functions and const generics

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

Rust
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 ::: in foo<i32>(x) the parser reads foo < i32 > (x). The turbofish removes the ambiguity. In types there is no such conflict, so Vec<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 of Results into a Result of a Vec.
  • sum and product need it when the element type does not decide the result (for example summing &i32 items into an i64).
  • 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 ? in main.
  • 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.