diff options
| author | kennytm <kennytm@gmail.com> | 2018-10-12 22:04:00 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-10-12 22:04:00 +0800 |
| commit | 8455468d0d0a253e5c0bbce5410b6976c84dbd02 (patch) | |
| tree | 00c8ee5ff503fd1806a906d6bc2ce86fc3031d71 | |
| parent | 849a0e9c40ef79efec0802334fe10406ea3e7256 (diff) | |
| parent | 68236e088d8b5694eb4097c1ad8302b92e36fd4a (diff) | |
| download | rust-8455468d0d0a253e5c0bbce5410b6976c84dbd02.tar.gz rust-8455468d0d0a253e5c0bbce5410b6976c84dbd02.zip | |
Rollup merge of #54755 - lucasloisp:document-reference-address-eq, r=QuietMisdreavus
Documents reference equality by address (#54197) Clarification of the use of `ptr::eq` to test equality of references via address by pointer coercion, regarding issue #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)
| -rw-r--r-- | src/libstd/primitive_docs.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs index 8d54728a75f..3b432d05132 100644 --- a/src/libstd/primitive_docs.rs +++ b/src/libstd/primitive_docs.rs @@ -908,11 +908,36 @@ mod prim_usize { } /// `&mut T` references can be freely coerced into `&T` references with the same referent type, and /// references with longer lifetimes can be freely coerced into references with shorter ones. /// +/// Reference equality by address, instead of comparing the values pointed to, is accomplished via +/// implicit reference-pointer coercion and raw pointer equality via [`ptr::eq`], while +/// [`PartialEq`] compares values. +/// +/// [`ptr::eq`]: ptr/fn.eq.html +/// [`PartialEq`]: cmp/trait.PartialEq.html +/// +/// ``` +/// 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!(five_ref == other_five_ref); +/// +/// assert!(ptr::eq(five_ref, same_five_ref)); +/// assert!(!ptr::eq(five_ref, other_five_ref)); +/// ``` +/// /// For more information on how to use references, see [the book's section on "References and /// Borrowing"][book-refs]. /// /// [book-refs]: ../book/second-edition/ch04-02-references-and-borrowing.html /// +/// # Trait implementations +/// /// The following traits are implemented for all `&T`, regardless of the type of its referent: /// /// * [`Copy`] |
