当用作函数的参数类型或返回类型时,“impl"是什么意思? [英] What does `impl` mean when used as the argument type or return type of a function?

查看:63
本文介绍了当用作函数的参数类型或返回类型时,“impl"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了这段代码:

pub fn up_to(limit: u64) ->impl 生成器{动||{对于 x 在 0..limit {产量 x;}退货限额;}}

impl 是什么意思?这如何在普通的 Rust 或 C++ 中实现?

解决方案

这是新的 impl Trait 语法,允许程序员避免命名泛型类型.该功能从 Rust 1.26 开始可用.>

在这里,它用于返回位置,表示返回的类型将实现此特征,这就是我要告诉您的全部内容".在这种情况下,请注意函数的所有返回路径必须返回完全相同的具体类型.

语法也可以用在参数位置,在这种情况下,调用者决定传递什么具体类型.

另见:

I read this code:

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:

这篇关于当用作函数的参数类型或返回类型时,“impl"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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