类型提示中_的正确术语是什么? [英] What is the correct term for _ in a type hint?

查看:102
本文介绍了类型提示中_的正确术语是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In type hints in Rust it is possible to use partial types in annotations like this:

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

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

What is the correct terminology for the underscore in the partial type annotation? I'm interested in both the Rust terminology as well as more academic type theory terminology.

推荐答案

经过一番挖掘,看来Vec<_>始终被称为部分类型(因此在let x: Vec<_>中,我们有部分类型注释,而Fn(String) -> _会是部分类型签名),但是在这种情况下,_被不同地称为类型通配符或类型占位符,并且类型语法中的_可以被理解为推断此类型"的标记(当时下面提到的PR,TyInfer在编译器内部).

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).

一些有趣的读物:

  • 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"

PR的有趣细节:

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

上面的两行是等效的,并且都被解析为类型为TyInfer的变量x.

The two lines above are equivalent, and both parsed as variable x with type TyInfer.

这篇关于类型提示中_的正确术语是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆