2013-05-02 14 views
6

Dal tutorial on borrowed pointers (rotto), un po 'modificato:Posso prendere in prestito un puntatore a un tratto condiviso in 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); 
} 

E tutto va bene, perché la casella condiviso è preso in prestito automaticamente per la funzione.

Ma fare la stessa cosa con un tratto:

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 
} 

E non riesce, (compilatore versione 0.6) dicendo:

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

Si tratta di un bug nel compilatore? Oppure i puntatori presi in prestito non sono consentiti per i tratti?

Se la risposta è il secondo, perché?

+0

Sono un po 'sorpreso dal messaggio di errore che hai segnalato, dal momento che stai lavorando con @TPoint e & TPoint nel codice, ma il messaggio di errore segnala un problema con ~ TPoint e & TPoint. (Sospetto un errore di trascrizione che potresti voler correggere.) – pnkfelix

+0

@pnkfelix: Effettivamente, corretto. Lo stesso accade con '~ TPoint' ma il messaggio non corrisponde al codice. – rodrigo

risposta

Problemi correlati