"Box< Fn()+发送+'static>"是什么意思?是指生锈? [英] What does "Box<Fn() + Send + 'static>" mean in rust?
问题描述
Box<Fn() + Send + 'static>
在锈中是什么意思?
在阅读高级类型一章时,我偶然发现了这种语法. Send
是一个特征,但是在类型参数化中,+
对特征(在此情况下为'static
)的生存期意味着什么?还有什么是Fn()
?
I stumbled upon this syntax while reading advanced types chapter. Send
is a trait but what does it mean to +
a lifetime to a trait ('static
in this case) in type parametrization ? Also what is Fn()
?
推荐答案
让我们一一分解它.
Box<T>
是指向堆分配的T
的指针.我们在这里使用它是因为特征对象只能存在于指针后面.
Box<T>
is a pointer to heap-allocated T
. We use it here because trait objects can only exist behind pointers.
在Box<Fn() + Send + 'static>
中,Fn() + Send + 'static
是 特征对象 类型.将来,它将写成
In Box<Fn() + Send + 'static>
, Fn() + Send + 'static
is a trait object type. In future, it will be written Box<dyn (Fn() + Send + 'static)>
to avoid confusion.
内部dyn
是对原始类型的限制.仅当T: Fn() + Send + 'static
时, Box<T>
才能被强制为Box<Fn() + Send + 'static>
.因此,尽管我们不知道原始类型,但我们可以假定它是Fn()
和Send
,并且具有'static
生存期.
Inside dyn
are restrictions to the original type. Box<T>
can be coerced into Box<Fn() + Send + 'static>
only when T: Fn() + Send + 'static
. Therefore, although we don't know the original type, we can assume it was Fn()
and Send
and had 'static
lifetime.
这是一个特征,就像Clone
或Default
一样.但是,它使用特殊的语法糖.
This is a trait, just like Clone
or Default
. However, it uses a special syntax sugar.
-
Fn(A1, ..., An)
是Fn<(A1, ..., An), Output=()>
的语法糖. -
Fn(A1, ..., An) -> R
是Fn<(A1, ..., An), Output=R>
的语法糖. - 此语法糖也适用于以下特征:
Fn
,FnMut
,FnBox
.
Fn(A1, ..., An)
is a syntax sugar forFn<(A1, ..., An), Output=()>
.Fn(A1, ..., An) -> R
is a syntax sugar forFn<(A1, ..., An), Output=R>
.- This syntax sugar also applies to the following traits:
Fn
,FnMut
,FnOnce
, andFnBox
.
那么Fn
是什么意思? T: Fn(A1, ..., An) -> R
表示x: T
是带有参数A1, ..., An
并返回类型R
的可调用对象.示例包括函数指针和闭包.
So what does Fn
mean? T: Fn(A1, ..., An) -> R
means x: T
is a callable object with arguments A1, ..., An
and return type R
. Examples include function pointers and closures.
Send
表示该类型的值可以跨线程发送.由于这是自动特征,它可以可以指定为dyn
类型的第二个边界 (特征对象类型).
Send
means that values of this type can be sent across threads. Since this is an auto trait, it can be specified as the second bounds of dyn
types (trait object types).
实际上, dyn
类型(特征对象类型)必须精确地绑定一个生存期.省略时可以推断出来.推理规则在 RFC 0192 和
In fact, dyn
types (trait object types) must have exactly one lifetime bound. It's inferred when omitted. The inference rule is described in RFC 0192 and RFC 1156. It's basically as follows:
- 如果明确给出,则使用该生存期.
- 否则,它是从内在特质推断出来的.例如,
Box<Any>
是Box<Any + 'static>
,因为Any: 'static
. - 如果特征没有适当的生存期,则可以从外部类型推断出它.例如,
&'a Fn()
是&'a (Fn() + 'a)
. - 如果甚至失败,它会退回到
'static
(用于函数签名)或匿名生存期(用于函数主体).
- If explicitly given, use that lifetime.
- Otherwise, it is inferred from the inner trait. For example,
Box<Any>
isBox<Any + 'static>
becauseAny: 'static
. - If the trait doesn't have an appropriate lifetime, it is inferred from the outer type. For example,
&'a Fn()
is&'a (Fn() + 'a)
. - If that even failed, it falls back to
'static
(for a function signature) or an anonymous lifetime (for a function body).
结论
f: Box<Fn() + Send + 'static>
是指向可调用值(原始类型未知且动态更改)的专有指针,例如闭包(无自变量或无返回值),这些值可以在线程之间发送,并且只要程序存在就可以使用本身.
Conclusion
f: Box<Fn() + Send + 'static>
is an owned pointer to a callable value (with the original type unknown and dynamically change) such as closures (with no argument or no return value), which can be sent across threads and lives as long as the program itself.
这篇关于"Box< Fn()+发送+'static>"是什么意思?是指生锈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!