Ho riscontrato un problema durante l'utilizzo dei parametri di durata per le strutture. Non sono sicuro al 100% come descrivere il problema, ma ho creato un caso banale che mostra il mio errore di compilazione.Errore di durata della ruggine previsto per il ciclo di vita del calcestruzzo ma durata illimitata prefissata
struct Ref;
struct Container<'a> {
r : &'a Ref
}
struct ContainerB<'a> {
c : Container<'a>
}
trait ToC {
fn to_c<'a>(&self, r : &'a Ref) -> Container<'a>;
}
impl<'a> ToC for ContainerB<'a> {
fn to_c(&self, r : &'a Ref) -> Container<'a> {
self.c
}
}
L'errore che sto ottenendo con questo è
test.rs:16:3: 18:4 error: method `to_c` has an incompatible type for trait: expected concrete lifetime, but found bound lifetime parameter 'a
test.rs:16 fn to_c(&self, r : &'a Ref) -> Container<'a> {
test.rs:17 self.c
test.rs:18 }
test.rs:16:48: 18:4 note: expected concrete lifetime is the lifetime 'a as defined on the block at 16:47
test.rs:16 fn to_c(&self, r : &'a Ref) -> Container<'a> {
test.rs:17 self.c
test.rs:18 }
error: aborting due to previous error
Ho provato molte variazioni e proprio non riesco a ottenere questa cosa per compilare. Ho trovato un altro post qui (How to fix: expected concrete lifetime, but found bound lifetime parameter) ma sembra aggirare il problema invece di risolverlo. Non riesco davvero a capire perché il problema abbia origine. Il & Ref viene passato attraverso le mosse quindi dovrebbe funzionare correttamente?
Qualche idea? Grazie per tutto l'aiuto.
Un'altra correzione sarebbe 'tratto ToC <'a> {fn to_c (& self, r: & 'a Ref) -> Contenitore <'a>; } '. – huon
Oh sì, quindi "impl <'a> ToC <'a> per ContainerB <'a>". –
Questo sicuramente aiuta molto e risolve alcuni dei miei pensieri sbagliati (ad esempio l'ombreggiamento a vita)! Grazie. Questo risolve questo caso al 100%, ma non riesco a applicarlo a un altro. Ho scritto questa domanda qui: http://stackoverflow.com/questions/24853111/rust-lifetime-error-expected-concrete-lifetime-part-2) – luke