Skip to main content

How to Use the Turbofish (::<>) in Rust

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Some Rust functions, such as parse, can produce many types, and Rust needs to know which one you want. The turbofish, ::<>, names it right in the call: "42".parse::<i32>().

Syntax​

Syntax
value.method::<Type>()
let name: Type = value.method();
PartWhat it does
value.methodA generic method, such as parse or collect, called on a value.
::<Type>The turbofish: the type the method should produce, such as i32.
let name: TypeThe other way to say the same thing: annotate the variable, and Rust works out the method's type from it.

The Rust Reference: "The :: token is required before the opening < for generic arguments to avoid ambiguity with the less-than operator. This is colloquially known as “turbofish” syntax." The documentation for str::parse: "parse is one of the few times you’ll see the syntax affectionately known as the ‘turbofish’: ::<>. This helps the inference algorithm understand specifically which type you’re trying to parse into."

The Example​

The example parses the text "42" three times: into an i32 and an f64 with the turbofish, and into a u8 with a type annotation.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.

Step 1: Create a Project​

Terminal
cargo new demo
cd demo
Output
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.

Step 2: Write the Code​

Replace the contents of src/main.rs with:

src/main.rs, which parses "42" into a as an i32 and into b as an f64 with the turbofish, and into c with a u8 type annotation, unwraps each, and prints all three.

src/main.rs
fn main() {
let a = "42".parse::<i32>().unwrap();
let b = "42".parse::<f64>().unwrap();
let c: u8 = "42".parse().unwrap();

println!("{a} {b} {c}");
}

unwrap() takes the number out of the Result that parse returns. c has no turbofish: its annotation, u8, tells parse what to produce.

Step 3: Run It​

Terminal
cargo run -q
Output
42 42 42

A zsh terminal where cargo run -q prints 42 42 42.

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. The three values print alike, but they're three types: a 32-bit integer, a 64-bit floating-point number and an 8-bit unsigned integer.

Without Either​

With neither a turbofish nor an annotation, Rust can't pick a type and stops the build:

src/main.rs
fn main() {
let a = "42".parse().unwrap();
println!("{a}");
}
Output
error[E0284]: type annotations needed
--> src/main.rs:2:9
|
2 | let a = "42".parse().unwrap();
| ^ ----- type must be known at this point
|
= note: cannot satisfy `<_ as FromStr>::Err == _`
= note: the type must also implement `FromStr`
note: required by a bound in `core::str::<impl str>::parse`
--> /rustc/0ef1bc386059596dada8fb65b50425aab2ea637a/library/core/src/str/mod.rs:2776:4
help: consider giving `a` an explicit type
|
2 | let a: /* Type */ = "42".parse().unwrap();
| ++++++++++++

For more information about this error, try `rustc --explain E0284`.
error: could not compile `demo` (bin "demo") due to 1 previous error

The help line suggests the annotation; the turbofish fixes it as well.

Collect into a Vec​

collect is the other method you'll often see with a turbofish, because it can build many kinds of collections. _ lets Rust fill in the element type:

src/main.rs
fn main() {
let words = "a b c".split(' ').collect::<Vec<_>>();
println!("{words:?}");
}
Output
["a", "b", "c"]

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the ::, as in parse<i32>()error: comparison operators cannot be chained, with the help use `::<...>` instead of `<...>` to specify lifetime, type, or const argumentsWrite parse::<i32>()
Neither a turbofish nor an annotationerror[E0284]: type annotations neededAdd one of them
Expecting the turbofish to convert a valueIt only picks the type parse produces; text that isn't a number still failsHandle the Err that parse returns

Troubleshooting​

SymptomLikely causeFix
type annotations neededRust can't infer a generic typeAdd a turbofish or a type annotation
comparison operators cannot be chained< read as less-thanPut :: before <

FAQ​

Turbofish or type annotation?​

They do the same job. The turbofish keeps the type next to the call, which helps in a chain such as .collect::<Vec<_>>(). The annotation reads well when you store the result in a variable anyway.

Why the name?​

::<> looks like a fish. The Reference calls the name colloquial, and the parse documentation "affectionately known".

Conclusion​

Write ::<Type> after a generic method's name, as in parse::<i32>(), to tell it what type to produce, or annotate the variable instead. When the compiler says "type annotations needed", one of the two is missing.

Additional Resources​