私は、Java/JavaScriptのバックグラウンドから生まれたRustを始めたばかりです。私は生涯の理解に何かが欠けているので、私に負担をかけてください。生涯には何か不足していますか?
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);
}
コンパイラはother_appearance
に達し、これはanother_thing
は十分に長く住んでいないことを私に言って、私にコンパイルエラーを与えています。しかし、私がother_appearance
の作成を省いた場合、プログラムはコンパイルされて正常に動作します。なぜこのエラーが発生するのですか?
これは実際には '#[derive(PartialEq)]によって作成されたインスタンスがOPと同じ問題を抱えているので興味深いです。 –
これとは逆に、値が宣言されている順序と逆の順序でドロップされるため、順序を入れ替えることで修正することもできます。つまり、 'appearance == other_appearance'の代わりに' other_appearance == appearance'を使います。はい、錆はいくつかの疣贅を持っています... –
ありがとう!私はあなたが言ったことを概念的に理解していても、構文にとどまる必要があります:) –