如何调用数组或元组类型的关联函数?
我可以为数组定义一个关联函数 foo
,如下所示:
pub trait T {
fn foo();
}
impl<X> T for [X; 2] {
fn foo() { panic!("Whatever") }
}
但是我现在如何调用这个函数呢?我注意到像 [usize;2]::foo()
这样的语法是无效的。
I can define an associated function foo
for an array like this:
pub trait T {
fn foo();
}
impl<X> T for [X; 2] {
fn foo() { panic!("Whatever") }
}
But how do I now call this function? I noticed that syntax like [usize;2]::foo()
is invalid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用完全验证的语法
&lt; type作为特质&gt; :: function()
。这是您的示例:在。
You can use the fully qualifidied syntax
<Type as Trait>::function()
. Here is your example:See it on the playground.
简短答案:您需要使用角括号:
&lt; [usize; 2]&gt; :: foo()
。长答案:
如果您可以看参考,您会看到语法:
empersy ,您有 pathexpression 。有两种路径表达 s: pathinexpression 和 合格Pathinexpression 。 pathinexpression 是表单的简单路径
a :: b :: b :: c ::&lt; generics&gt; :: d :: d ::&lt; generics&gt;
等。这就是什么当您键入EgString :: New()
或String :: From(“ ABC”)
时,则使用。合格的Pathinexpression ,另一方面,具有合格的Pathtype (
::
pathexprements )+) sup>,或简单的单词, cliquifiedpathtype ,然后是两个或多个实例,然后是两个结肠的实例,然后是 pathexpresegement 。 pathExpregent 被定义为一个名称(标识符),然后是两个结肠和仿制药:ie,foo
或foo ::&lt; a,a,b,b,c&gt; < /代码>。
什么是合格的Pathtype ?它被定义为
&lt;
type (AS
typepath )?&gt;
,或以角度括号包含的类型和a typepath ,与 PathExpression 非常相似。类型是任何生锈类型语法:一个路径(例如
a :: b&lt; generics&gt;
或a :: b :: b ::&lt; gen&lt; generics&gt;
,特征对象(dyn Trait
),Impl特征
,元组((T1,T2,T2,T3,...)
),数组(<代码> [t; n] ),切片([t]
),参考(&amp; t
或&amp; mut t
有可能使用寿命),所有这些都意味着您调用方法时,除非方法类型是一个简单的路径 - 也就>有可能使用仿制药,您必须将角度括号中的类型封闭 -
&lt; type&gt; :: method()指针(
fn(args) - &gt; ret
)等。 &lt; [usize; 2]&gt; :: foo()当然是使用此形式的另一个理由: disAmbuition),您必须使用表单
&lt; type作为特征&gt; :: method()
(这是“( as as typepath )< sup>?“我们看到了)。Short answer: You need to use angle brackets:
<[usize; 2]>::foo()
.Long answer:
If you'll look in the reference, you'll see the syntax:
Inside Expression, you have PathExpression. There are two kinds of PathExpression s: PathInExpression and QualifiedPathInExpression. PathInExpression is a simple path of the form
a::b::c::<generics>::d::<generics>
etc.. This is what you use when you type e.g.String::new()
orString::from("abc")
.QualifiedPathInExpression, on the other hand, has the form QualifiedPathType (
::
PathExprSegment)+, or in simple words, QualifiedPathType followed by one or more instances of two colons then PathExprSegement. PathExprSegment is defined as a name (identifier) optionally followed by two colons and generics: i.e.,foo
orfoo::<a, b, c>
.And what is QualifiedPathType ? It is defined as
<
Type (as
TypePath)?>
, or a type enclosed in angle brackets, optionally followed byas
and a TypePath, which is very similar to PathExpression with some differences.Type is any Rust type syntax: a path (e.g.
a::b<generics>
ora::b::<generics>
, trait objects (dyn Trait
),impl Trait
, tuples ((T1, T2, T3, ...)
), arrays ([T; N]
), slices ([T]
), references (&T
or&mut T
, potentially with a lifetime), and more.All of this means that when you call a method, unless the method type is a simple path - that is, has the form
a::b::c
, potentially with generics, you must enclose the type in angle brackets -<Type>::method()
. That includes arrays, slices, references, tuples, function pointers (fn(Args) -> Ret
), and more.[usize; 2]::foo()
is not a valid Rust syntax, only<[usize; 2]>::foo()
is.Of course, there is another reason to use this form: UFCS (Universal Function Call Syntax). When you want to specify the trait (usually for disambiguition), you must use the form
<Type as Trait>::method()
(this is the "(as
TypePath)?" we saw).