2013-05-02 81 views
6

tutorial on borrowed pointers(碎),比特修改:我可以借用一個指向Rust的共享特徵的指針嗎?

struct Point {x: float, y: float} 

fn compute(p1 : &Point) {} 

fn main() { 
    let shared_box : @Point = @Point {x: 5.0, y: 1.0}; 
    compute(shared_box); 
} 

所有的罰款,因爲共享盒自動借用的功能。

但與性狀做同樣的:

struct Point {x: float, y: float} 
trait TPoint {} 

impl TPoint for Point {} 

fn compute(p1 : &TPoint) {} 

fn main() { 
    let shared_box : @TPoint = @Point {x: 5.0, y: 1.0} as @TPoint; 

    compute(shared_box); 
    //  ^~~~~~~ The error is here 
} 

它失敗了,(編譯器版本0.6)說:

error: mismatched types: expected &TPoint but found @TPoint (trait storage differs: expected & but found @)

這是在編譯器中的錯誤?或者借用指針不允許使用特徵?

如果答案是後者,那爲什麼?

+0

由於您在代碼中使用了@TPoint和&TPoint,但是錯誤消息報告了〜TPoint和&TPoint的問題,所以對於您報告的錯誤消息,我感到有些驚訝。 (我懷疑你可能想糾正一個轉錄錯誤。) – pnkfelix 2013-05-02 21:49:18

+0

@pnkfelix:的確,更正了。 '〜TPoint'也是如此,但是消息與代碼不符。 – rodrigo 2013-05-02 21:56:30

回答

相關問題