什么是Vec< _>? [英] What is Vec<_>?

查看:118
本文介绍了什么是Vec< _>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过 Vec< _> >几次了。例如:

  let a =line1\r\\\
line2\\\
line3;
println!({:?},a.lines()。collect ::< Vec<>>());

但是,'不确定的脸'是什么?< _> 是什么意思?

我习惯于使用尖括号中的类型名称,但是可以使用哪种类型?
我注意到的下划线的唯一含义是来自Python,作为未使用变量的名称。

解决方案

它意味着Rust编译器,推断什么类型进入 Vec 。这实际上类似于Python中的未使用变量(以及Rust本身),因为它表示一个类型的占位符,就像它可以表示变量名的占位符。



您可以在迭代器编程语言章节中找到解释消费者


使用_可让您提供部分提示:



let one_to_one_hundred =(1..101).collect ::< Vec<>>(); 这就是
Collect into a Vec< T> ,但请推断 T 对我来说是什么。因为这个原因, _
有时被称为类型占位符。


I have seen Vec<_> a couple of times already. For example:

let a = "line1\r\nline2\nline3";
println!("{:?}", a.lines().collect::<Vec<_>>());

But what does that 'uncertain face' <_> mean?

I'm used to a typename in angle brackets, but what type can that be? The only meaning of underscore that I'm aware of is from Python as a name for an unused variable.

解决方案

It means "Rust compiler, infer what type goes into the Vec". And it is indeed analogous to the unused variable in Python (and in Rust itself), in that it represents a placeholder for a type, like it can represent a placeholder for a variable name.

You can find an explanation in The Rust Programming Language chapter about iterator consumers:

Using a _ will let you provide a partial hint:

let one_to_one_hundred = (1..101).collect::<Vec<_>>(); This says "Collect into a Vec<T>, please, but infer what the T is for me." _ is sometimes called a "type placeholder" for this reason.

这篇关于什么是Vec&lt; _&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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