標準庫提供了一個特殊的特性,[`Deref`](http://doc.rust-lang.org/nightly/std/ops/trait.Deref.html)。它一般用來重載`*`,解引用運算符:
~~~
use std::ops::Deref;
struct DerefExample<T> {
value: T,
}
impl<T> Deref for DerefExample<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
fn main() {
let x = DerefExample { value: 'a' };
assert_eq!('a', *x);
}
~~~
這對編寫自定義指針類型很有用。然而,這里有一個與`Deref`相關的語言功能:“解引用強制多態(deref coercions)”。規則如下:如果你有一個`U`類型,和它的實現`Deref`,(那么)`&U`的值將會自動轉換為`&T`。這是一個例子:
~~~
fn foo(s: &str) {
// borrow a string for a second
}
// String implements Deref<Target=str>
let owned = "Hello".to_string();
// therefore, this works:
foo(&owned);
~~~
在一個值的前面用`&`號獲取它的引用。所以`owned`是一個`String`,`&owned`是一個`&String`,而因為`impl Deref for String`,`&String`將會轉換為`&str`,而它是`foo()`需要的。
這就是了。這是Rust唯一一個為你進行一個自動轉換的地方,不過它增加了很多靈活性。例如,`Rc`類型實現了`Deref`,所以這可以工作:
~~~
use std::rc::Rc;
fn foo(s: &str) {
// borrow a string for a second
}
// String implements Deref<Target=str>
let owned = "Hello".to_string();
let counted = Rc::new(owned);
// therefore, this works:
foo(&counted);
~~~
我們所做的一切就是把我們的`String`封裝到了一個`Rc`里。不過現在我們可以傳遞`Rc`給任何我們有一個`String`的地方。`foo`的簽名并無變化,不過它對這兩個類型都能正常工作。這個例子有兩個轉換:`Rc`轉換為`String`接著是`String`轉換為`&str`。只要類型匹配Rust將可以做任意多次這樣的轉換。
標準庫提供的另一個非常通用的實現是:
~~~
fn foo(s: &[i32]) {
// borrow a slice for a second
}
// Vec<T> implements Deref<Target=[T]>
let owned = vec![1, 2, 3];
foo(&owned);
~~~
向量可以`Deref`為一個片段。
## `Deref`和方法調用
當調用一個方法時`Deref`也會出現。換句話說,這兩個(應該是`T`和`&T`)在Rust中是一樣的:
~~~
struct Foo;
impl Foo {
fn foo(&self) { println!("Foo"); }
}
let f = Foo;
f.foo();
~~~
即便`f`不是一個引用,而`foo`獲取`&self`,這也是可以工作的。因為這些都是一樣的:
~~~
f.foo();
(&f).foo();
(&&f).foo();
(&&&&&&&&f).foo();
~~~
一個`&&&&&&&&&&&&&&&&Foo`類型的值仍然可以調用`Foo`定義的方法,因為編譯器會插入足夠多的`*`來使類型正確。而正因為它插入`*`,它用了`Deref`。
- 前言
- 1.介紹
- 2.準備
- 2.1.安裝Rust
- 2.2.Hello, world!
- 2.3.Hello, Cargo!
- 3.學習Rust
- 3.1.猜猜看
- 3.2.哲學家就餐問題
- 3.3.其它語言中的Rust
- 4.高效Rust
- 4.1.棧和堆
- 4.2.測試
- 4.3.條件編譯
- 4.4.文檔
- 4.5.迭代器
- 4.6.并發
- 4.7.錯誤處理
- 4.8.外部語言接口
- 4.9.Borrow 和 AsRef
- 4.10.發布途徑
- 5.語法和語義
- 5.1.變量綁定
- 5.2.函數
- 5.3.原生類型
- 5.4.注釋
- 5.5.If語句
- 5.6.for循環
- 5.7.while循環
- 5.8.所有權
- 5.9.引用和借用
- 5.10.生命周期
- 5.11.可變性
- 5.12.結構體
- 5.13.枚舉
- 5.14.匹配
- 5.15.模式
- 5.16.方法語法
- 5.17.Vectors
- 5.18.字符串
- 5.19.泛型
- 5.20.Traits
- 5.21.Drop
- 5.22.if let
- 5.23.trait對象
- 5.24.閉包
- 5.25.通用函數調用語法
- 5.26.包裝箱和模塊
- 5.27.`const`和`static`
- 5.28.屬性
- 5.29.`type`別名
- 5.30.類型轉換
- 5.31.關聯類型
- 5.32.不定長類型
- 5.33.運算符和重載
- 5.34.`Deref`強制多態
- 5.35.宏
- 5.36.裸指針
- 6.Rust開發版
- 6.1.編譯器插件
- 6.2.內聯匯編
- 6.3.不使用標準庫
- 6.4.固有功能
- 6.5.語言項
- 6.6.鏈接參數
- 6.7.基準測試
- 6.8.裝箱語法和模式
- 6.9.切片模式
- 6.10.關聯常量
- 7.詞匯表
- 8.學院派研究
- 勘誤