# 通用函數調用語法
> [ufcs.md](https://github.com/rust-lang/rust/blob/master/src/doc/book/ufcs.md)
commit 024aa9a345e92aa1926517c4d9b16bd83e74c10d
有時,函數可能有相同的名字。就像下面這些代碼:
~~~
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"); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
我們需要一個區分我們需要調用哪一函數的方法。這個功能叫做“通用函數調用語法”(universal function call syntax),這看起來像這樣:
~~~
# 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;
Foo::f(&b);
Bar::f(&b);
~~~
讓我們拆開來看。
~~~
Foo::
Bar::
~~~
調用的這一半是兩個traits的類型:`Foo`和`Bar`。這樣實際上就區分了這兩者:Rust調用你使用的trait里面的方法。
~~~
f(&b)
~~~
當我們使用[方法語法](#)調用像`b.f()`這樣的方法時,如果`f()`需要`&self`,Rust實際上會自動地把`b`借用為`&self`。而在這個例子中,Rust并不會這么做,所以我們需要顯式地傳遞一個`&b`。
### 尖括號形式(Angle-bracket Form)
我們剛才討論的通用函數調用語法的形式:
~~~
Trait::method(args);
~~~
上面的形式其實是一種縮寫。這是在一些情況下需要使用的擴展形式:
~~~
<Type as Trait>::method(args);
~~~
`<>::`語法是一個提供類型提示的方法。類型位于`<>`中。在這個例子中,類型是`Type as Trait`,表示我們想要`method`的`Trait`版本被調用。在沒有二義時`as Trait`部分是可選的。尖括號也是一樣。因此上面的形式就是一種縮寫的形式。
這是一個使用較長形式的例子。
~~~
trait Foo {
fn foo() -> i32;
}
struct Bar;
impl Bar {
fn foo() -> i32 {
20
}
}
impl Foo for Bar {
fn foo() -> i32 {
10
}
}
fn main() {
assert_eq!(10, <Bar as Foo>::foo());
assert_eq!(20, Bar::foo());
}
~~~
使用尖括號語法讓你可以調用指定trait的方法而不是繼承到的那個。
- 前言
- 貢獻者
- 1.介紹
- 2.準備
- 3.學習 Rust
- 3.1.猜猜看
- 3.2.哲學家就餐問題
- 3.3.其它語言中的 Rust
- 4.語法和語義
- 4.1.變量綁定
- 4.2.函數
- 4.3.原生類型
- 4.4.注釋
- 4.5.If語句
- 4.6.循環
- 4.7.所有權
- 4.8.引用和借用
- 4.9.生命周期
- 4.10.可變性
- 4.11.結構體
- 4.12.枚舉
- 4.13.匹配
- 4.14.模式
- 4.15.方法語法
- 4.16.Vectors
- 4.17.字符串
- 4.18.泛型
- 4.19.Traits
- 4.20.Drop
- 4.21.if let
- 4.22.trait 對象
- 4.23.閉包
- 4.24.通用函數調用語法
- 4.25.crate 和模塊
- 4.26.const和static
- 4.27.屬性
- 4.28.type別名
- 4.29.類型轉換
- 4.30.關聯類型
- 4.31.不定長類型
- 4.32.運算符和重載
- 4.33.Deref強制多態
- 4.34.宏
- 4.35.裸指針
- 4.36.不安全代碼
- 5.高效 Rust
- 5.1.棧和堆
- 5.2.測試
- 5.3.條件編譯
- 5.4.文檔
- 5.5.迭代器
- 5.6.并發
- 5.7.錯誤處理
- 5.8.選擇你的保證
- 5.9.外部函數接口
- 5.10.Borrow 和 AsRef
- 5.11.發布途徑
- 5.12.不使用標準庫
- 6.Rust 開發版
- 6.1.編譯器插件
- 6.2.內聯匯編
- 6.4.固有功能
- 6.5.語言項
- 6.6.鏈接進階
- 6.7.基準測試
- 6.8.裝箱語法和模式
- 6.9.切片模式
- 6.10.關聯常量
- 6.11.自定義內存分配器
- 7.詞匯表
- 8.語法索引
- 9.參考文獻
- 附錄:名詞中英文對照