类型提示中 _ 的正确术语是什么?

What is the correct term for _ in a type hint?

在 Rust 的类型提示中,可以像这样在注释中使用部分类型:

let myvec: Vec<_> = vec![1, 2, 3];

部分类型注释中下划线的正确术语是什么?我对 Rust 术语以及更多学术类型理论术语都很感兴趣。

我找到了一个 piece of official documentation,其中下划线是在模式的上下文中命名的,但我怀疑它是一个 "strict" 名称:

Patterns consist of some combination of literals, destructured arrays or enum constructors, structs and tuples, variable binding specifications, wildcards (..), and placeholders (_).

The Book 在词汇表中提供以下描述:

_: "ignored" pattern binding (see Patterns (Ignoring bindings)). Also used to make integer-literals readable (see Reference (Integer literals)).

我找不到特别指向部分类型注释的定义,但我认为"placeholder"(或"type placeholder",取决于上下文) 不会有歧义。

编译器里好像叫Infer(在syntax::ast, rustc::hir, and rustc::ty中)

我认为这个命名有些合理,因为在进行类似 Hindley-Milner 的类型推断之前,这些 _ 被替换为新鲜的 (type) 推断变量

经过一些挖掘,似乎 Vec<_> 一直被称为部分类型(所以在 let x: Vec<_> 中我们有一个部分类型注释,而 Fn(String) -> _ 将是一个部分类型签名)但是 _ 在这种情况下被称为类型通配符或类型占位符,类型语法中的 _ 可以被读取为 "infer this type" 的标记(在下面提到的PR,TyInfer在编译器内部)。

一些有趣的阅读:

PR 中有趣的细节:

let x: _ = 5;
let x    = 5;

上面两行是等价的,都被解析为类型为 TyInfer.

的变量 x

似乎语法将其称为“推断类型”。每 the documentation:

Inferred type

Syntax:

InferredType : _

The inferred type asks the compiler to infer the type if possible based on the surrounding information available. It cannot be used in item signatures. It is often used in generic arguments:

let x: Vec<_> = (0..10).collect();