我有以下代码段,由于值匹配的返回引用,我被迫在匹配块中使用neg == &true
。我可以像if neg {}
而不是if neg == &true {}
这样用更简洁的方式写吗?如果没有,为什么?
pub enum AtomNumeric {
Integer(bool, u64),
Rational(bool, u64, u64),
Real(bool, u64, u64, u64),
}
impl fmt::Display for AtomNumeric {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
AtomNumeric::Integer(neg, b) => {
if neg == &true {
write!(f, "-{}", b)
} else {
write!(f, "+{}", b)
}
}
AtomNumeric::Rational(neg, num, den) => {
if neg == &true {
write!(f, "-{}/{}", num, den)
} else {
write!(f, "+{}/{}", num, den)
}
}
AtomNumeric::Real(neg, lead, zeros, dec) => {
if neg == &true {
write!(f, "-{}-{}*10^{}", lead, dec, zeros)
} else {
write!(f, "+{}+{}*10^{}", lead, dec, zeros)
}
}
}
}
}