Trait core::cmp::PartialEq

1.0.0 · source ·
pub trait PartialEq<Rhs: ?Sized = Self> {
    // Required method
    fn eq(&self, other: &Rhs) -> bool;

    // Provided method
    fn ne(&self, other: &Rhs) -> bool { ... }
}
Expand description

Trait 等值比较。

x.eq(y) 也可以写成 x == yx.ne(y),也可以写成 x != y。 在本文档的其余部分中,我们使用了更易于阅读的中缀符号。

对于没有完全等价关系的类型,这个 trait 允许部分相等。 例如,在浮点数 NaN != NaN 中,因此浮点类型实现 PartialEq,但不实现 Eq。 形式上来说,当 Rhs == Self 时,这个 trait 对应一个 partial equivalence relation

实现必须确保 eqne 彼此一致:

  • a != b 当且仅当 !(a == b)

ne 的默认实现提供了这种一致性,并且几乎总是足够的。没有很好的理由不应该覆盖它。

如果 SelfRhs 也实现了 PartialOrdOrd,则它们的方法也必须与 PartialEq 一致 (具体要求请参见那些 traits 的文档)。 通过派生一些 traits 并手动实现其他一些行为,很容易使它们不以为然。

等式关系 == 必须满足以下条件 (对于所有类型为 ABCabc) :

  • 对称: 如果 A: PartialEq<B>B: PartialEq<A>,则 a == b 意味着 ’b == a`; 和

  • 可传递: 如果 A: PartialEq<B>B: PartialEq<C> 以及 A: PartialEq<C>,然后 a == b,并且 b == c 暗示了 a == c

请注意,B: PartialEq<A> (symmetric) 和 A: PartialEq<C> (transitive) 强制不是强制存在的,但是这些要求只要存在就适用。

Derivable

该 trait 可以与 #[derive] 一起使用。在结构体上 derive d 时,如果所有字段都相等,则两个实例相等; 如果任何字段不相等,则两个实例不相等。当在枚举上 派生 时,如果两个实例是相同的变体并且所有字段都相等,则它们是相等的。

如何实现 PartialEq

一个域的示例实现,在该域中,即使两本书的 ISBN 匹配,即使格式不同,也将其视为同一本书:

enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

struct Book {
    isbn: i32,
    format: BookFormat,
}

impl PartialEq for Book {
    fn eq(&self, other: &Self) -> bool {
        self.isbn == other.isbn
    }
}

let b1 = Book { isbn: 3, format: BookFormat::Paperback };
let b2 = Book { isbn: 3, format: BookFormat::Ebook };
let b3 = Book { isbn: 10, format: BookFormat::Paperback };

assert!(b1 == b2);
assert!(b1 != b3);
Run

如何比较两种不同的类型?

您可以比较的类型由 PartialEq 的类型参数控制。 例如,让我们对之前的代码进行一些调整:

// 衍生工具 <BookFormat> == <BookFormat> 比较
#[derive(PartialEq)]
enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

struct Book {
    isbn: i32,
    format: BookFormat,
}

// 实现 <Book> == <BookFormat> 比较
impl PartialEq<BookFormat> for Book {
    fn eq(&self, other: &BookFormat) -> bool {
        self.format == *other
    }
}

// 实现 <BookFormat> == <Book> 比较
impl PartialEq<Book> for BookFormat {
    fn eq(&self, other: &Book) -> bool {
        *self == other.format
    }
}

let b1 = Book { isbn: 3, format: BookFormat::Paperback };

assert!(b1 == BookFormat::Paperback);
assert!(BookFormat::Ebook != b1);
Run

通过将 impl PartialEq for Book 更改为 impl PartialEq<BookFormat> for Book,我们可以将 BookFormat 和 Book 进行比较。

像上面这样的比较 (它忽略了结构体的某些字段) 可能很危险。这很容易导致意外违反部分对等关系的要求。 例如,如果我们保留了以上针对 BookFormatPartialEq<Book> 的实现,并为 Book 添加了 PartialEq<Book> 的实现 (通过 #[derive] 或第一个示例中的手动实现),则结果将违反传递性:

#[derive(PartialEq)]
enum BookFormat {
    Paperback,
    Hardback,
    Ebook,
}

#[derive(PartialEq)]
struct Book {
    isbn: i32,
    format: BookFormat,
}

impl PartialEq<BookFormat> for Book {
    fn eq(&self, other: &BookFormat) -> bool {
        self.format == *other
    }
}

impl PartialEq<Book> for BookFormat {
    fn eq(&self, other: &Book) -> bool {
        *self == other.format
    }
}

fn main() {
    let b1 = Book { isbn: 1, format: BookFormat::Paperback };
    let b2 = Book { isbn: 2, format: BookFormat::Paperback };

    assert!(b1 == BookFormat::Paperback);
    assert!(BookFormat::Paperback == b2);

    // 以下应该通过传递性来保持,但不是。
    assert!(b1 == b2); // <-- PANICS
}
Run

Examples

let x: u32 = 0;
let y: u32 = 1;

assert_eq!(x == y, false);
assert_eq!(x.eq(&y), false);
Run

Required Methods§

source

fn eq(&self, other: &Rhs) -> bool

此方法测试 selfother 值是否相等,并由 == 使用。

Provided Methods§

source

fn ne(&self, other: &Rhs) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。

Implementors§

source§

impl PartialEq<AsciiChar> for AsciiChar

1.34.0 · source§

impl PartialEq<Infallible> for Infallible

1.28.0 · source§

impl PartialEq<Alignment> for core::fmt::Alignment

1.7.0 · source§

impl PartialEq<IpAddr> for IpAddr

1.16.0 · source§

impl PartialEq<IpAddr> for Ipv4Addr

1.16.0 · source§

impl PartialEq<IpAddr> for Ipv6Addr

source§

impl PartialEq<Ipv6MulticastScope> for Ipv6MulticastScope

source§

impl PartialEq<SocketAddr> for SocketAddr

source§

impl PartialEq<FpCategory> for FpCategory

1.55.0 · source§

impl PartialEq<IntErrorKind> for IntErrorKind

source§

impl PartialEq<Which> for Which

source§

impl PartialEq<SearchStep> for SearchStep

source§

impl PartialEq<Ordering> for core::sync::atomic::Ordering

source§

impl PartialEq<Ordering> for core::cmp::Ordering

source§

impl PartialEq<bool> for bool

source§

impl PartialEq<char> for char

source§

impl PartialEq<f32> for f32

source§

impl PartialEq<f64> for f64

source§

impl PartialEq<i8> for i8

source§

impl PartialEq<i16> for i16

source§

impl PartialEq<i32> for i32

source§

impl PartialEq<i64> for i64

source§

impl PartialEq<i128> for i128

source§

impl PartialEq<isize> for isize

source§

impl PartialEq<!> for !

source§

impl PartialEq<str> for str

source§

impl PartialEq<u8> for u8

source§

impl PartialEq<u16> for u16

source§

impl PartialEq<u32> for u32

source§

impl PartialEq<u64> for u64

source§

impl PartialEq<u128> for u128

source§

impl PartialEq<()> for ()

source§

impl PartialEq<usize> for usize

source§

impl PartialEq<AllocError> for AllocError

1.28.0 · source§

impl PartialEq<Layout> for Layout

1.50.0 · source§

impl PartialEq<LayoutError> for LayoutError

source§

impl PartialEq<TypeId> for TypeId

1.27.0 · source§

impl PartialEq<CpuidResult> for CpuidResult

Available on x86 or x86-64 only.
1.34.0 · source§

impl PartialEq<CharTryFromError> for CharTryFromError

1.9.0 · source§

impl PartialEq<DecodeUtf16Error> for DecodeUtf16Error

1.20.0 · source§

impl PartialEq<ParseCharError> for ParseCharError

1.59.0 · source§

impl PartialEq<TryFromCharError> for TryFromCharError

source§

impl PartialEq<CStr> for CStr

1.69.0 · source§

impl PartialEq<FromBytesUntilNulError> for FromBytesUntilNulError

1.64.0 · source§

impl PartialEq<FromBytesWithNulError> for FromBytesWithNulError

source§

impl PartialEq<Error> for Error

1.33.0 · source§

impl PartialEq<PhantomPinned> for PhantomPinned

source§

impl PartialEq<Assume> for Assume

source§

impl PartialEq<AddrParseError> for AddrParseError

1.16.0 · source§

impl PartialEq<Ipv4Addr> for IpAddr

source§

impl PartialEq<Ipv4Addr> for Ipv4Addr

1.16.0 · source§

impl PartialEq<Ipv6Addr> for IpAddr

source§

impl PartialEq<Ipv6Addr> for Ipv6Addr

source§

impl PartialEq<SocketAddrV4> for SocketAddrV4

source§

impl PartialEq<SocketAddrV6> for SocketAddrV6

1.34.0 · source§

impl PartialEq<NonZeroI8> for NonZeroI8

1.34.0 · source§

impl PartialEq<NonZeroI16> for NonZeroI16

1.34.0 · source§

impl PartialEq<NonZeroI32> for NonZeroI32

1.34.0 · source§

impl PartialEq<NonZeroI64> for NonZeroI64

1.34.0 · source§

impl PartialEq<NonZeroI128> for NonZeroI128

1.34.0 · source§

impl PartialEq<NonZeroIsize> for NonZeroIsize

1.28.0 · source§

impl PartialEq<NonZeroU8> for NonZeroU8

1.28.0 · source§

impl PartialEq<NonZeroU16> for NonZeroU16

1.28.0 · source§

impl PartialEq<NonZeroU32> for NonZeroU32

1.28.0 · source§

impl PartialEq<NonZeroU64> for NonZeroU64

1.28.0 · source§

impl PartialEq<NonZeroU128> for NonZeroU128

1.28.0 · source§

impl PartialEq<NonZeroUsize> for NonZeroUsize

source§

impl PartialEq<ParseFloatError> for ParseFloatError

source§

impl PartialEq<ParseIntError> for ParseIntError

1.34.0 · source§

impl PartialEq<TryFromIntError> for TryFromIntError

source§

impl PartialEq<RangeFull> for RangeFull

source§

impl PartialEq<Alignment> for core::ptr::Alignment

source§

impl PartialEq<ParseBoolError> for ParseBoolError

source§

impl PartialEq<Utf8Error> for Utf8Error

1.36.0 · source§

impl PartialEq<RawWaker> for RawWaker

1.36.0 · source§

impl PartialEq<RawWakerVTable> for RawWakerVTable

1.3.0 · source§

impl PartialEq<Duration> for Duration

1.66.0 · source§

impl PartialEq<TryFromFloatSecsError> for TryFromFloatSecsError

1.10.0 · source§

impl<'a> PartialEq<Location<'a>> for Location<'a>

source§

impl<'a> PartialEq<Utf8Chunk<'a>> for Utf8Chunk<'a>

source§

impl<A, B> PartialEq<[B]> for [A]where A: PartialEq<B>,

source§

impl<A, B, const N: usize> PartialEq<&[B]> for [A; N]where A: PartialEq<B>,

source§

impl<A, B, const N: usize> PartialEq<&mut [B]> for [A; N]where A: PartialEq<B>,

source§

impl<A, B, const N: usize> PartialEq<[A; N]> for &[B]where B: PartialEq<A>,

source§

impl<A, B, const N: usize> PartialEq<[A; N]> for &mut [B]where B: PartialEq<A>,

source§

impl<A, B, const N: usize> PartialEq<[A; N]> for [B]where B: PartialEq<A>,

source§

impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]where A: PartialEq<B>,

source§

impl<A, B, const N: usize> PartialEq<[B]> for [A; N]where A: PartialEq<B>,

source§

impl<A, B: ?Sized> PartialEq<&B> for &Awhere A: PartialEq<B> + ?Sized,

source§

impl<A, B: ?Sized> PartialEq<&B> for &mut Awhere A: PartialEq<B> + ?Sized,

source§

impl<A, B: ?Sized> PartialEq<&mut B> for &Awhere A: PartialEq<B> + ?Sized,

source§

impl<A, B: ?Sized> PartialEq<&mut B> for &mut Awhere A: PartialEq<B> + ?Sized,

1.55.0 · source§

impl<B: PartialEq, C: PartialEq> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C>

source§

impl<Dyn: ?Sized> PartialEq<DynMetadata<Dyn>> for DynMetadata<Dyn>

1.4.0 · source§

impl<F: FnPtr> PartialEq<F> for F

1.29.0 · source§

impl<H> PartialEq<BuildHasherDefault<H>> for BuildHasherDefault<H>

source§

impl<Idx: PartialEq> PartialEq<Range<Idx>> for Range<Idx>

source§

impl<Idx: PartialEq> PartialEq<RangeFrom<Idx>> for RangeFrom<Idx>

1.26.0 · source§

impl<Idx: PartialEq> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx>

source§

impl<Idx: PartialEq> PartialEq<RangeTo<Idx>> for RangeTo<Idx>

1.26.0 · source§

impl<Idx: PartialEq> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx>

1.41.0 · source§

impl<P: Deref, Q: Deref> PartialEq<Pin<Q>> for Pin<P>where P::Target: PartialEq<Q::Target>,

source§

impl<T> PartialEq<(T,)> for (T₁, T₂, …, Tₙ)where T: ?Sized + PartialEq,

This trait is implemented for tuples up to twelve items long.

1.21.0 · source§

impl<T> PartialEq<Discriminant<T>> for Discriminant<T>

source§

impl<T, const LANES: usize> PartialEq<Mask<T, LANES>> for Mask<T, LANES>where T: MaskElement + PartialEq, LaneCount<LANES>: SupportedLaneCount,

source§

impl<T, const N: usize> PartialEq<Simd<T, N>> for Simd<T, N>where LaneCount<N>: SupportedLaneCount, T: SimdElement + PartialEq,

source§

impl<T: PartialEq + Copy> PartialEq<Cell<T>> for Cell<T>

1.20.0 · source§

impl<T: PartialEq + ?Sized> PartialEq<ManuallyDrop<T>> for ManuallyDrop<T>

1.17.0 · source§

impl<T: PartialEq> PartialEq<Bound<T>> for Bound<T>

source§

impl<T: PartialEq> PartialEq<Option<T>> for Option<T>

1.36.0 · source§

impl<T: PartialEq> PartialEq<Poll<T>> for Poll<T>

1.70.0 · source§

impl<T: PartialEq> PartialEq<OnceCell<T>> for OnceCell<T>

source§

impl<T: PartialEq> PartialEq<Saturating<T>> for Saturating<T>

source§

impl<T: PartialEq> PartialEq<Wrapping<T>> for Wrapping<T>

1.19.0 · source§

impl<T: PartialEq> PartialEq<Reverse<T>> for Reverse<T>

source§

impl<T: PartialEq, E: PartialEq> PartialEq<Result<T, E>> for Result<T, E>

source§

impl<T: ?Sized + PartialEq> PartialEq<RefCell<T>> for RefCell<T>

source§

impl<T: ?Sized> PartialEq<*const T> for *const T

source§

impl<T: ?Sized> PartialEq<*mut T> for *mut T

source§

impl<T: ?Sized> PartialEq<PhantomData<T>> for PhantomData<T>

1.25.0 · source§

impl<T: ?Sized> PartialEq<NonNull<T>> for NonNull<T>

source§

impl<Y: PartialEq, R: PartialEq> PartialEq<GeneratorState<Y, R>> for GeneratorState<Y, R>