Skip to content

Commit 8455468

Browse files
authored
Rollup merge of rust-lang#54755 - lucasloisp:document-reference-address-eq, r=QuietMisdreavus
Documents reference equality by address (rust-lang#54197) Clarification of the use of `ptr::eq` to test equality of references via address by pointer coercion, regarding issue rust-lang#54197 . The same example as in `ptr::eq` docs is shown here to clarify that `PartialEq` compares values pointed-to instead of via address (which can be desired in some cases)
2 parents 849a0e9 + 68236e0 commit 8455468

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/libstd/primitive_docs.rs

+25
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,36 @@ mod prim_usize { }
908908
/// `&mut T` references can be freely coerced into `&T` references with the same referent type, and
909909
/// references with longer lifetimes can be freely coerced into references with shorter ones.
910910
///
911+
/// Reference equality by address, instead of comparing the values pointed to, is accomplished via
912+
/// implicit reference-pointer coercion and raw pointer equality via [`ptr::eq`], while
913+
/// [`PartialEq`] compares values.
914+
///
915+
/// [`ptr::eq`]: ptr/fn.eq.html
916+
/// [`PartialEq`]: cmp/trait.PartialEq.html
917+
///
918+
/// ```
919+
/// use std::ptr;
920+
///
921+
/// let five = 5;
922+
/// let other_five = 5;
923+
/// let five_ref = &five;
924+
/// let same_five_ref = &five;
925+
/// let other_five_ref = &other_five;
926+
///
927+
/// assert!(five_ref == same_five_ref);
928+
/// assert!(five_ref == other_five_ref);
929+
///
930+
/// assert!(ptr::eq(five_ref, same_five_ref));
931+
/// assert!(!ptr::eq(five_ref, other_five_ref));
932+
/// ```
933+
///
911934
/// For more information on how to use references, see [the book's section on "References and
912935
/// Borrowing"][book-refs].
913936
///
914937
/// [book-refs]: ../book/second-edition/ch04-02-references-and-borrowing.html
915938
///
939+
/// # Trait implementations
940+
///
916941
/// The following traits are implemented for all `&T`, regardless of the type of its referent:
917942
///
918943
/// * [`Copy`]

0 commit comments

Comments
 (0)