core::ptr::eq - Rust
Description: API documentation for the Rust `eq` fn in crate `core`.
Keywords: rust, rustlang, rust-lang, eq

# 1.17.0[−][src]Function core::ptr::eq

    pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool

Compares raw pointers for equality.

This is the same as using the `==` operator, but less generic: the arguments have to be `*const T` raw pointers, not anything that implements `PartialEq`.

This can be used to compare `&T` references (which coerce to `*const T` implicitly) by their address rather than comparing the values they point to (which is what the `PartialEq for &T` implementation does).

# Examples

    use std::ptr;

    let five = 5;
    let other_five = 5;
    let five_ref = &five;
    let same_five_ref = &five;
    let other_five_ref = &other_five;

    assert!(five_ref == same_five_ref);
    assert!(ptr::eq(five_ref, same_five_ref));

    assert!(five_ref == other_five_ref);
    assert!(!ptr::eq(five_ref, other_five_ref));Run

Slices are also compared by their length (fat pointers):

    let a = [1, 2, 3];
    assert!(std::ptr::eq(&a[..3], &a[..3]));
    assert!(!std::ptr::eq(&a[..2], &a[..3]));
    assert!(!std::ptr::eq(&a[0..2], &a[1..3]));Run

Traits are also compared by their implementation:

    #[repr(transparent)]
    struct Wrapper { member: i32 }

    trait Trait {}
    impl Trait for Wrapper {}
    impl Trait for i32 {}

    let wrapper = Wrapper { member: 10 };

    // Pointers have equal addresses.
    assert!(std::ptr::eq(
        &wrapper as *const Wrapper as *const u8,
        &wrapper.member as *const i32 as *const u8
    ));

    // Objects have equal addresses, but `Trait` has different implementations.
    assert!(!std::ptr::eq(
        &wrapper as &dyn Trait,
        &wrapper.member as &dyn Trait,
    ));
    assert!(!std::ptr::eq(
        &wrapper as &dyn Trait as *const dyn Trait,
        &wrapper.member as &dyn Trait as *const dyn Trait,
    ));

    // Converting the reference to a `*const u8` compares by address.
    assert!(std::ptr::eq(
        &wrapper as &dyn Trait as *const dyn Trait as *const u8,
        &wrapper.member as &dyn Trait as *const dyn Trait as *const u8,
    ));Run