问题描述
pub fn up_to(limit: u64) ->impl 生成器{动||{对于 x 在 0..limit {产量 x;}退货限额;}}
impl
是什么意思?这如何在普通的 Rust 或 C++ 中实现?
这是新的 impl Trait
语法,允许程序员避免命名泛型类型.该功能从 Rust 1.26 开始可用.>
在这里,它用于返回位置,表示返回的类型将实现此特征,这就是我要告诉您的全部内容".在这种情况下,请注意函数的所有返回路径必须返回完全相同的具体类型.
语法也可以用在参数位置,在这种情况下,调用者决定传递什么具体类型.
另见:
- 在 Trait 定义中使用 impl Trait
- impl trait 参数和泛型函数参数有什么区别?
- 是什么让 `impl Trait` 作为参数通用"?并作为返回值存在"?
- 返回迭代器(或任何其他特征)的正确方法是什么?
pub fn up_to(limit: u64) -> impl Generator<Yield = u64, Return = u64> {
move || {
for x in 0..limit {
yield x;
}
return limit;
}
}
What does impl
mean? How might this be implemented in plain Rust or C++?
This is the new impl Trait
syntax which allows the programmer to avoid naming generic types. The feature is available as of Rust 1.26.
Here, it is used in return position to say "the type returned will implement this trait, and that's all I'm telling you". In this case, note that all return paths of the function must return the exact same concrete type.
The syntax can also be used in argument position, in which case the caller decides what concrete type to pass.
See also:
- Using impl Trait in Trait definition
- What are the differences between an impl trait argument and generic function parameter?
- What makes `impl Trait` as an argument "universal" and as a return value "existential"?
- What is the correct way to return an Iterator (or any other trait)?
这篇关于当用作函数的参数类型或返回类型时,“impl"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!