Expand description
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));
RunFor 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:
Copy
Clone
(Note that this will not defer toT
’sClone
implementation if it exists!)Deref
Borrow
fmt::Pointer
&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:
- All the traits in
std::fmt
exceptfmt::Pointer
(which is implemented regardless of the type of its referent) andfmt::Write
PartialOrd
Ord
PartialEq
Eq
AsRef
Fn
(in addition,&T
references getFnMut
andFnOnce
ifT: Fn
)Hash
ToSocketAddrs
Send
(&T
references also requireT: Sync
)
&mut T
references get all of the above except ToSocketAddrs
, plus the following, if T
implements that trait:
AsMut
FnMut
(in addition,&mut T
references getFnOnce
ifT: FnMut
)fmt::Write
Iterator
DoubleEndedIterator
ExactSizeIterator
FusedIterator
TrustedLen
io::Write
Read
Seek
BufRead
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
1.2.0 · sourceimpl<'a, T> Extend<&'a T> for LinkedList<T> where
T: 'a + Copy,
impl<'a, T> Extend<&'a T> for LinkedList<T> where
T: 'a + Copy,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
1.2.0 · sourceimpl<'a, T, A> Extend<&'a T> for VecDeque<T, A> where
T: 'a + Copy,
A: Allocator,
impl<'a, T, A> Extend<&'a T> for VecDeque<T, A> where
T: 'a + Copy,
A: Allocator,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcepub fn extend_reserve(&mut self, additional: usize)
pub fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
1.2.0 · sourceimpl<'a, T, A> Extend<&'a T> for Vec<T, A> where
T: 'a + Copy,
A: 'a + Allocator,
impl<'a, T, A> Extend<&'a T> for Vec<T, A> where
T: 'a + Copy,
A: 'a + Allocator,
Extend implementation that copies elements out of references before pushing them onto the Vec.
This implementation is specialized for slice iterators, where it uses copy_from_slice
to
append the entire slice at once.
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcepub fn extend_reserve(&mut self, additional: usize)
pub fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
1.2.0 · sourceimpl<'a, T> Extend<&'a T> for BTreeSet<T> where
T: 'a + Ord + Copy,
impl<'a, T> Extend<&'a T> for BTreeSet<T> where
T: 'a + Ord + Copy,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
1.2.0 · sourceimpl<'a, T> Extend<&'a T> for BinaryHeap<T> where
T: 'a + Ord + Copy,
impl<'a, T> Extend<&'a T> for BinaryHeap<T> where
T: 'a + Ord + Copy,
sourcepub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
pub fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcepub fn extend_reserve(&mut self, additional: usize)
pub fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
1.4.0 · sourceimpl<'a, T, S> Extend<&'a T> for HashSet<T, S> where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
impl<'a, T, S> Extend<&'a T> for HashSet<T, S> where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
sourcefn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<K, Q: ?Sized, V, S> Index<&'_ Q> for HashMap<K, V, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
impl<K, Q: ?Sized, V, S> Index<&'_ Q> for HashMap<K, V, S> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
S: BuildHasher,
sourceimpl<'_, T, V> Join<&'_ T> for [V] where
T: Clone,
V: Borrow<[T]>,
impl<'_, T, V> Join<&'_ T> for [V] where
T: Clone,
V: Borrow<[T]>,
sourceimpl<'_, '_, A, B> PartialEq<&'_ mut B> for &'_ mut A where
A: PartialEq<B> + ?Sized,
B: ?Sized,
impl<'_, '_, A, B> PartialEq<&'_ mut B> for &'_ mut A where
A: PartialEq<B> + ?Sized,
B: ?Sized,
sourceimpl<'_, '_, A, B> PartialOrd<&'_ B> for &'_ A where
A: PartialOrd<B> + ?Sized,
B: ?Sized,
impl<'_, '_, A, B> PartialOrd<&'_ B> for &'_ A where
A: PartialOrd<B> + ?Sized,
B: ?Sized,
sourcepub fn partial_cmp(&self, other: &&B) -> Option<Ordering>
pub fn partial_cmp(&self, other: &&B) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcepub fn lt(&self, other: &&B) -> bool
pub fn lt(&self, other: &&B) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcepub fn le(&self, other: &&B) -> bool
pub fn le(&self, other: &&B) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<'_, '_, A, B> PartialOrd<&'_ mut B> for &'_ mut A where
A: PartialOrd<B> + ?Sized,
B: ?Sized,
impl<'_, '_, A, B> PartialOrd<&'_ mut B> for &'_ mut A where
A: PartialOrd<B> + ?Sized,
B: ?Sized,
sourcepub fn partial_cmp(&self, other: &&mut B) -> Option<Ordering>
pub fn partial_cmp(&self, other: &&mut B) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcepub fn lt(&self, other: &&mut B) -> bool
pub fn lt(&self, other: &&mut B) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcepub fn le(&self, other: &&mut B) -> bool
pub fn le(&self, other: &&mut B) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more