Movatterモバイル変換


[0]ホーム

URL:


codecamp

通用函数调用语法

通用函数调用语法

有时,函数可以有相同的名字。看看下面这段代码:

trait Foo {fn f(&self);}trait Bar {fn f(&self);}struct Baz;impl Foo for Baz {fn f(&self) { println!("Baz’s impl of Foo"); }}impl Bar for Baz {fn f(&self) { println!("Baz’s impl of Bar"); }}let b = Baz;

如果我们试图调用 b.f(),我们就会得到一个错误:

error: multiple applicable methods in scope [E0034]b.f();note: candidate #1 is defined in an impl of the trait `main::Foo` for the type`main::Baz`fn f(&self) { println!("Baz’s impl of Foo"); }note: candidate #2 is defined in an impl of the trait `main::Bar` for the type`main::Baz`fn f(&self) { println!("Baz’s impl of Bar"); }

我们需要一种消除歧义的方法。这种方法称为“通用函数调用语法”,它这种语法看起来是下面这样的:

Foo::f(&b);Bar::f(&b);我们让它停止一下。Foo::Bar::

这些部分调用的类型有两个特征:Foo 和 Bar。这最终实际上是做了两者之间的消歧:Rust 从两者中调用你所使用的特征名称。

f(&b)

当我们用method syntax()调用一个方法比如 b.f(),如果 f() 含有 &self,Rust 会自动 borrow b。在本例这种情况下,Rust 不会,所以我们需要传递一个具体的 &b。

尖括号形式

现在我们谈论一下 UFCS 形式:

Trait::method(args);

这只是一种速记收手法。下面是在某些情况下需要使用的扩展形式:

<Type as Trait>::method(args);

< >:: 语法是一种提供类型提示的方法。类型写在< >s 里面。在本例中,类型是 Type as Trait 表明我们希望特征的方法在这里被调用。在不出现歧义的情况下,特征部分是可选的。这同样适用于用尖括号括,因此要用较短的形式。

这是使用更长形式的一个例子:

    trait Foo {    fn clone(&self);    }    #[derive(Clone)]    struct Bar;    impl Foo for Bar {    fn clone(&self) {    println!("Making a clone of Bar");    <Bar as Clone>::clone(self);    }    }

这将调用 Clone 特征的 Clone () 方法,而不是 Foo 特征的方法。

闭包
箱和模块
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录
关于
介绍

新手入门

学习 Rust

高效 Rust

语法和语义

Nightly Rust

词汇表
相关学术研究

关闭

MIP.setData({'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false},'pageFontSize' : getCookie('pageFontSize') || 20});MIP.watch('pageTheme', function(newValue){setCookie('pageTheme', JSON.stringify(newValue))});MIP.watch('pageFontSize', function(newValue){setCookie('pageFontSize', newValue)});function setCookie(name, value){var days = 1;var exp = new Date();exp.setTime(exp.getTime() + days*24*60*60*1000);document.cookie = name + '=' + value + ';expires=' + exp.toUTCString();}function getCookie(name){var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null;}
[8]ページ先頭

©2009-2025 Movatter.jp