如何调用数组或元组类型的关联函数?

发布于 2025-01-20 11:29:26 字数 236 浏览 5 评论 0原文

我可以为数组定义一个关联函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2025-01-27 11:29:26

您可以使用完全验证的语法&lt; type作为特质&gt; :: function()。这是您的示例:

pub trait Trait {
    fn foo();
}

impl<X> Trait for [X; 2] {
    fn foo() {
        panic!("Whatever");
    }
}

fn main() {
    <[usize; 2] as Trait>::foo();
}

You can use the fully qualifidied syntax <Type as Trait>::function(). Here is your example:

pub trait Trait {
    fn foo();
}

impl<X> Trait for [X; 2] {
    fn foo() {
        panic!("Whatever");
    }
}

fn main() {
    <[usize; 2] as Trait>::foo();
}

See it on the playground.

星星的轨迹 2025-01-27 11:29:26

简短答案:您需要使用角括号:&lt; [usize; 2]&gt; :: foo()

长答案:

如果您可以看参考,您会看到语法:

callexpression
&nbsp;&nbsp; &nbsp;&nbsp; expession callparams


empersy ,您有 pathexpression 。有两种路径表达 s: pathinexpression 合格Pathinexpression pathinexpression 是表单的简单路径a :: b :: b :: c ::&lt; generics&gt; :: d :: d ::&lt; generics&gt;等。这就是什么当您键入Eg String :: New()String :: From(“ ABC”)时,则使用。

合格的Pathinexpression ,另一方面,具有合格的Pathtype :: pathexprements +) sup>,或简单的单词, cliquifiedpathtype ,然后是两个或多个实例,然后是两个结肠的实例,然后是 pathexpresegement pathExpregent 被定义为一个名称(标识符),然后是两个结肠和仿制药:ie,foofoo ::&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:

CallExpression :
      Expression ( CallParams? )

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() or String::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 or foo::<a, b, c>.

And what is QualifiedPathType ? It is defined as < Type (as TypePath)? >, or a type enclosed in angle brackets, optionally followed by as 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> or a::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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文