1.0.0[]Primitive Type reference

References, both shared and mutable.

A reference represents a borrow of some owned value. You can get one by using the & or &mut operators on a value, or by using a ref or ref mut pattern.

For those familiar with pointers, a reference is just a pointer that is assumed to be aligned, not null, and pointing to memory containing a valid value of T - for example, &bool can only point to an allocation containing the integer values 1 (true) or 0 (false), but creating a &bool that points to an allocation containing the value 3 causes undefined behaviour. In fact, Option<&T> has the same memory representation as a nullable but aligned pointer, and can be passed across FFI boundaries as such.

In most cases, references can be used much like the original value. Field access, method calling, and indexing work the same (save for mutability rules, of course). In addition, the comparison operators transparently defer to the referent's implementation, allowing references to be compared the same as owned values.

References have a lifetime attached to them, which represents the scope for which the borrow is valid. A lifetime is said to "outlive" another one if its representative scope is as long or longer than the other. The 'static lifetime is the longest lifetime, which represents the total life of the program. For example, string literals have a 'static lifetime because the text data is embedded into the binary of the program, rather than in an allocation that needs to be dynamically managed.

&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.

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));Run

For more information on how to use references, see the book's section on "References and Borrowing".

Trait implementations

The following traits are implemented for all &T, regardless of the type of its referent:

&mut T references get all of the above except Copy and Clone (to prevent creating multiple simultaneous mutable borrows), plus the following, regardless of the type of its referent:

The following traits are implemented on &T references if the underlying T also implements that trait:

&mut T references get all of the above except ToSocketAddrs, plus the following, if T implements that trait:

Note that due to method call deref coercion, simply calling a trait method will act like they work on references as well as they do on owned values! The implementations described here are meant for generic contexts, where the final type T is a type parameter or otherwise not locally known.

Trait Implementations

impl<'a, I> DoubleEndedIterator for &'a mut I where
    I: DoubleEndedIterator + ?Sized
[src]

impl<'_, A, F> FnMut<A> for &'_ mut F where
    F: FnMut<A> + ?Sized
[src]

impl<'_, A, F> FnMut<A> for &'_ F where
    F: Fn<A> + ?Sized
[src]

impl<'_, T> Debug for &'_ T where
    T: Debug + ?Sized
[src]

impl<'_, T> Debug for &'_ mut T where
    T: Debug + ?Sized
[src]

impl<'_, T> UpperHex for &'_ T where
    T: UpperHex + ?Sized
[src]

impl<'_, T> UpperHex for &'_ mut T where
    T: UpperHex + ?Sized
[src]

impl<'_, T> Octal for &'_ mut T where
    T: Octal + ?Sized
[src]

impl<'_, T> Octal for &'_ T where
    T: Octal + ?Sized
[src]

impl<'_, T> UpperExp for &'_ mut T where
    T: UpperExp + ?Sized
[src]

impl<'_, T> UpperExp for &'_ T where
    T: UpperExp + ?Sized
[src]

impl<'_, T> DerefMut for &'_ mut T where
    T: ?Sized
[src]

impl<'_, T> Hash for &'_ mut T where
    T: Hash + ?Sized
[src]

impl<'_, T> Hash for &'_ T where
    T: Hash + ?Sized
[src]

impl<'_, W> Write for &'_ mut W where
    W: Write + ?Sized
1.4.0[src]

impl<'_, A> Eq for &'_ A where
    A: Eq + ?Sized
[src]

impl<'_, A> Eq for &'_ mut A where
    A: Eq + ?Sized
[src]

impl<'_, T> BorrowMut<T> for &'_ mut T where
    T: ?Sized
[src]

impl<'_, T, U> AsMut<U> for &'_ mut T where
    T: AsMut<U> + ?Sized,
    U: ?Sized
[src]

impl<'_, G> Generator for &'_ mut G where
    G: Unpin + Generator + ?Sized
[src]

type Yield = <G as Generator>::Yield

🔬 This is a nightly-only experimental API. (generator_trait #43122)

The type of value this generator yields. Read more

type Return = <G as Generator>::Return

🔬 This is a nightly-only experimental API. (generator_trait #43122)

The type of value this generator returns. Read more

impl<'_, T> Deref for &'_ mut T where
    T: ?Sized
[src]

type Target = T

The resulting type after dereferencing.

impl<'_, T> Deref for &'_ T where
    T: ?Sized
[src]

type Target = T

The resulting type after dereferencing.

impl<'_, I> FusedIterator for &'_ mut I where
    I: FusedIterator + ?Sized
1.26.0[src]

impl<'_, T> Pointer for &'_ mut T where
    T: ?Sized
[src]

impl<'_, T> Pointer for &'_ T where
    T: ?Sized
[src]

impl<'_, T> Clone for &'_ T where
    T: ?Sized
[src]

impl<'_, A, F> Fn<A> for &'_ F where
    F: Fn<A> + ?Sized
[src]

impl<'_, '_, A, B> PartialEq<&'_ mut B> for &'_ A where
    A: PartialEq<B> + ?Sized,
    B: ?Sized
[src]

impl<'_, '_, A, B> PartialEq<&'_ B> for &'_ mut A where
    A: PartialEq<B> + ?Sized,
    B: ?Sized
[src]

impl<'_, '_, A, B> PartialEq<&'_ B> for &'_ A where
    A: PartialEq<B> + ?Sized,
    B: ?Sized
[src]

impl<'_, '_, A, B> PartialEq<&'_ mut B> for &'_ mut A where
    A: PartialEq<B> + ?Sized,
    B: ?Sized
[src]

impl<'_, F> Future for &'_ mut F where
    F: Unpin + Future + ?Sized
1.36.0[src]

type Output = <F as Future>::Output

The type of value produced on completion.

impl<'_, T> Binary for &'_ mut T where
    T: Binary + ?Sized
[src]

impl<'_, T> Binary for &'_ T where
    T: Binary + ?Sized
[src]

impl<'_, A, F> FnOnce<A> for &'_ F where
    F: Fn<A> + ?Sized
[src]

type Output = <F as FnOnce<A>>::Output

The returned type after the call operator is used.

impl<'_, A, F> FnOnce<A> for &'_ mut F where
    F: FnMut<A> + ?Sized
[src]

type Output = <F as FnOnce<A>>::Output

The returned type after the call operator is used.

impl<'_, H> Hasher for &'_ mut H where
    H: Hasher + ?Sized
1.22.0[src]

impl<'_, '_, A, B> PartialOrd<&'_ mut B> for &'_ mut A where
    A: PartialOrd<B> + ?Sized,
    B: ?Sized
[src]

impl<'_, '_, A, B> PartialOrd<&'_ B> for &'_ A where
    A: PartialOrd<B> + ?Sized,
    B: ?Sized
[src]

impl<'a, T, U> CoerceUnsized<&'a mut U> for &'a mut T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b mut T where
    'b: 'a,
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, T, U> CoerceUnsized<*mut U> for &'a mut T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, T, U> CoerceUnsized<*const U> for &'a mut T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b T where
    'b: 'a,
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, T, U> CoerceUnsized<*const U> for &'a T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'_, T> Send for &'_ mut T where
    T: Send + ?Sized
[src]

impl<'_, T> Send for &'_ T where
    T: Sync + ?Sized
[src]

impl<'a, T, U> DispatchFromDyn<&'a U> for &'a T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, T, U> DispatchFromDyn<&'a mut U> for &'a mut T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'_, I> ExactSizeIterator for &'_ mut I where
    I: ExactSizeIterator + ?Sized
[src]

impl<'a, T> Unpin for &'a T where
    T: 'a + ?Sized
1.33.0[src]

impl<'a, T> Unpin for &'a mut T where
    T: 'a + ?Sized
1.33.0[src]

impl<'_, T> Display for &'_ mut T where
    T: Display + ?Sized
[src]

impl<'_, T> Display for &'_ T where
    T: Display + ?Sized
[src]

impl<'_, T> LowerExp for &'_ T where
    T: LowerExp + ?Sized
[src]

impl<'_, T> LowerExp for &'_ mut T where
    T: LowerExp + ?Sized
[src]

impl<'_, I> Iterator for &'_ mut I where
    I: Iterator + ?Sized
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

impl<'_, T> Borrow<T> for &'_ mut T where
    T: ?Sized
[src]

impl<'_, T> Borrow<T> for &'_ T where
    T: ?Sized
[src]

impl<'_, A> Ord for &'_ A where
    A: Ord + ?Sized
[src]

impl<'_, A> Ord for &'_ mut A where
    A: Ord + ?Sized
[src]

impl<'_, T, U> AsRef<U> for &'_ mut T where
    T: AsRef<U> + ?Sized,
    U: ?Sized
[src]

impl<'_, T, U> AsRef<U> for &'_ T where
    T: AsRef<U> + ?Sized,
    U: ?Sized
[src]

impl<'_, I> TrustedLen for &'_ mut I where
    I: TrustedLen + ?Sized
[src]

impl<'_, T> LowerHex for &'_ T where
    T: LowerHex + ?Sized
[src]

impl<'_, T> LowerHex for &'_ mut T where
    T: LowerHex + ?Sized
[src]

impl<'_, T> Copy for &'_ T where
    T: ?Sized
[src]

impl<'_, R: Read + ?Sized> Read for &'_ mut R[src]

impl<'_, W: Write + ?Sized> Write for &'_ mut W[src]

impl<'_, S: Seek + ?Sized> Seek for &'_ mut S[src]

impl<'_, B: BufRead + ?Sized> BufRead for &'_ mut B[src]

impl<'_, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'_ T[src]

type Iter = T::Iter

Returned iterator over socket addresses which this type may correspond to. Read more

impl<'_, T: ?Sized> !UnwindSafe for &'_ mut T1.9.0[src]

impl<'_, T: RefUnwindSafe + ?Sized> UnwindSafe for &'_ T1.9.0[src]