How to Use the Turbofish (::<>) in Rust
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
value.method::<Type>()
let name: Type = value.method();
| Part | What it does |
|---|---|
value.method | A 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: Type | The 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
cargo new demo
cd demo
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:

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
cargo run -q
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:
fn main() {
let a = "42".parse().unwrap();
println!("{a}");
}
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:
fn main() {
let words = "a b c".split(' ').collect::<Vec<_>>();
println!("{words:?}");
}
["a", "b", "c"]
Common Mistakes
| Mistake | What happens | Do 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 arguments | Write parse::<i32>() |
| Neither a turbofish nor an annotation | error[E0284]: type annotations needed | Add one of them |
| Expecting the turbofish to convert a value | It only picks the type parse produces; text that isn't a number still fails | Handle the Err that parse returns |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
type annotations needed | Rust can't infer a generic type | Add a turbofish or a type annotation |
comparison operators cannot be chained | < read as less-than | Put :: 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
- Paths in expressions, in the Rust Reference.
- str::parse, in the standard library documentation.
