What is the correct term for _ in a type hint?
After some digging it seems that Vec<_>
is consistently called a partial type (so in let x: Vec<_>
we have a partial type annotation, while Fn(String) -> _
would be a partial type signature) but the _
in this context is varyingly called either a type wildcard or a type placeholder, and _
in the type grammar can be read as the token for "infer this type" (at the time of the PR mentioned below, TyInfer
internally in the compiler).
Some interesting reading:
- Partial type signatures in Haskell
- The pull request which added
_
to the Rust type grammar - Intermingled parameter lists - Niko Matsakis' blog post in which he proposes to "Introduce
_
as a notation for an unspecified lifetime or type"
Interesting detail from the PR:
let x: _ = 5;
let x = 5;
The two lines above are equivalent, and both parsed as variable x
with type TyInfer
.
I was able to find a piece of official documentation where the underscore is named in the context of patterns, but I doubt it's a "strict" name:
Patterns consist of some combination of literals, destructured arrays or enum constructors, structs and tuples, variable binding specifications, wildcards (..), and placeholders (_).
The Book provides the following description in the glossary:
_: "ignored" pattern binding (see Patterns (Ignoring bindings)). Also used to make integer-literals readable (see Reference (Integer literals)).
I was not able to find a definition pointing specifically to partial type annotations, but I think "placeholder" (or "type placeholder", depending on the context) would not be ambiguous.