什么是Vec< _>? [英] What is 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 aVec< 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 aVec<T>
, please, but infer what theT
is for me."_
is sometimes called a "type placeholder" for this reason.
这篇关于什么是Vec< _>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!