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 not be null. In fact, Option<&T> has the same memory representation as a nullable 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<'_, T> Deref for &'_ T where
    T: ?Sized
[src]

The resulting type after dereferencing.

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

The resulting type after dereferencing.

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

Performs copy-assignment from source. Read more

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

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

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

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

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

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

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

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

This is the reverse version of [try_fold()]: it takes elements starting from the back of the iterator. Read more

An iterator method that reduces the iterator's elements to a single, final value, starting from the back. Read more

Searches for an element of an iterator from the back that satisfies a predicate. Read more

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

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

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

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

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

impl<'a, 'b, A, B> PartialEq<&'b B> for &'a mut A where
    A: PartialEq<B> + ?Sized,
    B: ?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, '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<'a, T, U> CoerceUnsized<*mut U> for &'a mut T where
    T: Unsize<U> + ?Sized,
    U: ?Sized
[src]

impl<'a, T, U> CoerceUnsized<&'a 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<'_, T, U> AsRef<U> for &'_ T where
    T: AsRef<U> + ?Sized,
    U: ?Sized
[src]

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

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

impl<'_, A, F> FnMut<A> for &'_ mut F where
    F: FnMut<A> + ?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> TrustedLen for &'_ mut I where
    I: TrustedLen + ?Sized
[src]

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

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

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

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

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

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

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

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

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

Feeds a slice of this type into the given [Hasher]. Read more

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

Feeds a slice of this type into the given [Hasher]. Read more

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

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

The type of value this generator yields. Read more

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

The type of value this generator returns. Read more

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> Iterator for &'_ mut I where
    I: Iterator + ?Sized
[src]

The type of the elements being iterated over.

Consumes the iterator, counting the number of iterations and returning it. Read more

Consumes the iterator, returning the last element. Read more

Important traits for StepBy<I>

Creates an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Important traits for Chain<A, B>

Takes two iterators and creates a new iterator over both in sequence. Read more

Important traits for Zip<A, B>

'Zips up' two iterators into a single iterator of pairs. Read more

Important traits for Map<I, F>

Takes a closure and creates an iterator which calls that closure on each element. Read more

Calls a closure on each element of an iterator. Read more

Important traits for Filter<I, P>

Creates an iterator which uses a closure to determine if an element should be yielded. Read more

Important traits for FilterMap<I, F>

Creates an iterator that both filters and maps. Read more

Important traits for Enumerate<I>

Creates an iterator which gives the current iteration count as well as the next value. Read more

Important traits for Peekable<I>

Creates an iterator which can use peek to look at the next element of the iterator without consuming it. Read more

Important traits for SkipWhile<I, P>

Creates an iterator that [skip]s elements based on a predicate. Read more

Important traits for TakeWhile<I, P>

Creates an iterator that yields elements based on a predicate. Read more

Important traits for Skip<I>

Creates an iterator that skips the first n elements. Read more

Important traits for Take<I>

Creates an iterator that yields its first n elements. Read more

Important traits for Scan<I, St, F>

An iterator adaptor similar to [fold] that holds internal state and produces a new iterator. Read more

Important traits for FlatMap<I, U, F>

Creates an iterator that works like map, but flattens nested structure. Read more

Important traits for Flatten<I>

Creates an iterator that flattens nested structure. Read more

Important traits for Fuse<I>

Creates an iterator which ends after the first [None]. Read more

Important traits for Inspect<I, F>

Do something with each element of an iterator, passing the value on. Read more

Important traits for &'_ mut I

Borrows an iterator, rather than consuming it. Read more

Transforms an iterator into a collection. Read more

Consumes an iterator, creating two collections from it. Read more

An iterator method that applies a function as long as it returns successfully, producing a single, final value. Read more

An iterator method that applies a fallible function to each item in the iterator, stopping at the first error and returning that error. Read more

An iterator method that applies a function, producing a single, final value. Read more

Tests if every element of the iterator matches a predicate. Read more

Tests if any element of the iterator matches a predicate. Read more

Searches for an element of an iterator that satisfies a predicate. Read more

Applies function to the elements of iterator and returns the first non-none result. Read more

Searches for an element in an iterator, returning its index. Read more

Searches for an element in an iterator from the right, returning its index. Read more

Returns the maximum element of an iterator. Read more

Returns the minimum element of an iterator. Read more

Returns the element that gives the maximum value from the specified function. Read more

Returns the element that gives the maximum value with respect to the specified comparison function. Read more

Returns the element that gives the minimum value from the specified function. Read more

Returns the element that gives the minimum value with respect to the specified comparison function. Read more

Important traits for Rev<I>

Reverses an iterator's direction. Read more

Converts an iterator of pairs into a pair of containers. Read more

Important traits for Cloned<I>

Creates an iterator which [clone]s all of its elements. Read more

Important traits for Cycle<I>

Repeats an iterator endlessly. Read more

Sums the elements of an iterator. Read more

Iterates over the entire iterator, multiplying all the elements Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Lexicographically compares the elements of this Iterator with those of another. Read more

Determines if the elements of this Iterator are equal to those of another. Read more

Determines if the elements of this Iterator are unequal to those of another. Read more

Determines if the elements of this Iterator are lexicographically less than those of another. Read more

Determines if the elements of this Iterator are lexicographically less or equal to those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than those of another. Read more

Determines if the elements of this Iterator are lexicographically greater than or equal to those of another. Read more

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

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

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

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

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

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

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

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

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

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

🔬 This is a nightly-only experimental API. (futures_api #50547)

futures in libcore are unstable

The result of the Future.

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

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

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

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

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

The returned type after the call operator is used.

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

The returned type after the call operator is used.

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

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

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

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

impl<'_, T> DerefMut for &'_ mut T where
    T: ?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> Send for &'_ T where
    T: Sync + ?Sized
[src]

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

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

Important traits for &'_ mut I

Creates a "by reference" adaptor for this instance of Read. Read more

Important traits for Bytes<R>

Transforms this Read instance to an [Iterator] over its bytes. Read more

Important traits for Chain<T, U>

Creates an adaptor which will chain this stream with another. Read more

Important traits for Take<T>

Creates an adaptor which will read at most limit bytes from it. Read more

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

Important traits for &'_ mut I

Creates a "by reference" adaptor for this instance of Write. Read more

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

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

Important traits for Split<B>

Returns an iterator over the contents of this reader split on the byte byte. Read more

Important traits for Lines<B>

Returns an iterator over the lines of this reader. Read more

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

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

impl<'a, T: ?Sized> !UnwindSafe for &'a mut T
1.9.0
[src]

impl<'a, T: RefUnwindSafe + ?Sized> UnwindSafe for &'a T
1.9.0
[src]