简体   繁体   English

使用FnOnce定义特征,但没有返回类型

[英]Define a trait with FnOnce, but no return type

I'd like to define a trait like so (to avoid repetition later): 我想像这样定义一个特征(以避免以后重复):

trait Callback: FnOnce() + Send {}

However, the compiler demands that I define Output from FnOnce: 但是,编译器要求我从FnOnce定义Output:

error: the value of the associated type Output (from the trait core::ops::FnOnce ) must be specified [E0191] 错误:必须指定相关类型Output (来自trait core::ops::FnOnce )的值[E0191]

I tried to default the value, but it warns that this is unstable. 我试图默认值,但它警告这是不稳定的。

type Output = ();

error: associated type defaults are unstable 错误:关联类型默认值不稳定

What can I define Output as to indicate "No return"? 我可以定义什么输出表示“不返回”? The normal function call syntax simply omits it. 普通函数调用语法只是省略了它。

You can fix this by being explicit about the return type: 您可以通过明确返回类型来解决此问题:

trait Callback: FnOnce() -> () + Send {}

I'm honestly not sure if this is a bug or not. 我老实说不确定这是不是一个bug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM