Ich habe gerade begonnen, Rust zu lernen, aus einem Java/JavaScript-Hintergrund, also ertragen Sie mit mir, weil ich offensichtlich etwas in meinem Verständnis von Lebenszeiten vermisse.Fehle ich etwas, wenn es um Lebenszeiten geht?
fn main() {
struct Appearance<'a> {
identity: &'a u64,
role: &'a str
};
impl<'a> PartialEq for Appearance<'a> {
fn eq(&self, other: &Appearance) -> bool {
self.identity == other.identity && self.role == other.role
}
};
let thing = 42u64;
let hair_color = "hair color";
let appearance = Appearance {
identity: &thing,
role: &hair_color
};
let another_thing = 43u64;
let other_appearance = Appearance {
identity: &another_thing,
role: &hair_color
};
println!("{}", appearance == other_appearance);
}
Das gibt mir einen Übersetzungsfehler wie der Compiler die other_appearance
erreicht hat, mir zu sagen, dass another_thing
nicht lange genug lebt. Wenn ich jedoch die Erstellung von other_appearance
weglassen, kompiliert das Programm und läuft gut. Warum erhalte ich diesen Fehler?
Das ist eigentlich interessant, weil die Instanz, die von '# [derived (PartialEq)]' erzeugt wird, das gleiche Problem hat wie OPs. –
Da die Werte in der umgekehrten Reihenfolge fallen, in der sie deklariert werden, können Sie dies auch beheben, indem Sie die Reihenfolge vertauschen, d. H. 'Other_apperance == aussehen 'anstelle von' aussehen == other_apperance'. Ja, Rust hat ein paar Warzen ... –
Danke! Ich muss mich auf die Syntax konzentrieren, auch wenn ich konzeptionell verstehe, was du gesagt hast :) –