2013-05-02 38 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

回答

相关问题