Primitive Type reference
1.0.0 ·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));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:
CopyClone(Note that this will not defer toT’sCloneimplementation if it exists!)DerefBorrowfmt::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::fmtexceptfmt::Pointer(which is implemented regardless of the type of its referent) andfmt::Write PartialOrdOrdPartialEqEqAsRefFn(in addition,&Treferences getFnMutandFnOnceifT: Fn)HashToSocketAddrsSend(&Treferences also requireT: Sync)
&mut T references get all of the above except ToSocketAddrs, plus the following, if T
implements that trait:
AsMutFnMut(in addition,&mut Treferences getFnOnceifT: FnMut)fmt::WriteIteratorDoubleEndedIteratorExactSizeIteratorFusedIteratorTrustedLenio::WriteReadSeekBufRead
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.
Implementations
source impl<'a, T> Pin<&'a T> where
T: ?Sized,
impl<'a, T> Pin<&'a T> where
T: ?Sized,
1.33.0 · source pub unsafe fn map_unchecked<U, F>(self, func: F) -> Pin<&'a U>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output; where
F: FnOnce(&T) -> &U,
U: ?Sized,
pub unsafe fn map_unchecked<U, F>(self, func: F) -> Pin<&'a U>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output; where
F: FnOnce(&T) -> &U,
U: ?Sized,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Constructs a new pin by mapping the interior value.
For example, if you wanted to get a Pin of a field of something,
you could use this to get access to that field in one line of code.
However, there are several gotchas with these "pinning projections";
see the pin module documentation for further details on that topic.
Safety
This function is unsafe. You must guarantee that the data you return will not move so long as the argument value does not move (for example, because it is one of the fields of that value), and also that you do not move out of the argument you receive to the interior function.
1.33.0 (const: unstable) · source pub fn get_ref(self) -> &'a T
pub fn get_ref(self) -> &'a T
Gets a shared reference out of a pin.
This is safe because it is not possible to move out of a shared reference.
It may seem like there is an issue here with interior mutability: in fact,
it is possible to move a T out of a &RefCell<T>. However, this is
not a problem as long as there does not also exist a Pin<&T> pointing
to the same data, and RefCell<T> does not let you create a pinned reference
to its contents. See the discussion on "pinning projections" for further
details.
Note: Pin also implements Deref to the target, which can be used
to access the inner value. However, Deref only provides a reference
that lives for as long as the borrow of the Pin, not the lifetime of
the Pin itself. This method allows turning the Pin into a reference
with the same lifetime as the original Pin.
source impl<'a, T> Pin<&'a mut T> where
T: ?Sized,
impl<'a, T> Pin<&'a mut T> where
T: ?Sized,
1.33.0 (const: unstable) · source pub fn into_ref(self) -> Pin<&'a T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
pub fn into_ref(self) -> Pin<&'a T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Converts this Pin<&mut T> into a Pin<&T> with the same lifetime.
1.33.0 (const: unstable) · source pub fn get_mut(self) -> &'a mut T where
T: Unpin,
pub fn get_mut(self) -> &'a mut T where
T: Unpin,
Gets a mutable reference to the data inside of this Pin.
This requires that the data inside this Pin is Unpin.
Note: Pin also implements DerefMut to the data, which can be used
to access the inner value. However, DerefMut only provides a reference
that lives for as long as the borrow of the Pin, not the lifetime of
the Pin itself. This method allows turning the Pin into a reference
with the same lifetime as the original Pin.
1.33.0 (const: unstable) · source pub unsafe fn get_unchecked_mut(self) -> &'a mut T
pub unsafe fn get_unchecked_mut(self) -> &'a mut T
Gets a mutable reference to the data inside of this Pin.
Safety
This function is unsafe. You must guarantee that you will never move
the data out of the mutable reference you receive when you call this
function, so that the invariants on the Pin type can be upheld.
If the underlying data is Unpin, Pin::get_mut should be used
instead.
1.33.0 · source pub unsafe fn map_unchecked_mut<U, F>(self, func: F) -> Pin<&'a mut U>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output; where
F: FnOnce(&mut T) -> &mut U,
U: ?Sized,
pub unsafe fn map_unchecked_mut<U, F>(self, func: F) -> Pin<&'a mut U>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output; where
F: FnOnce(&mut T) -> &mut U,
U: ?Sized,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Construct a new pin by mapping the interior value.
For example, if you wanted to get a Pin of a field of something,
you could use this to get access to that field in one line of code.
However, there are several gotchas with these "pinning projections";
see the pin module documentation for further details on that topic.
Safety
This function is unsafe. You must guarantee that the data you return will not move so long as the argument value does not move (for example, because it is one of the fields of that value), and also that you do not move out of the argument you receive to the interior function.
source impl<T> Pin<&'static T> where
T: ?Sized,
impl<T> Pin<&'static T> where
T: ?Sized,
1.61.0 (const: unstable) · source pub fn static_ref(r: &'static T) -> Pin<&'static T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
pub fn static_ref(r: &'static T) -> Pin<&'static T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Get a pinned reference from a static reference.
This is safe, because T is borrowed for the 'static lifetime, which
never ends.
source impl<T> Pin<&'static mut T> where
T: ?Sized,
impl<T> Pin<&'static mut T> where
T: ?Sized,
1.61.0 (const: unstable) · source pub fn static_mut(r: &'static mut T) -> Pin<&'static mut T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
pub fn static_mut(r: &'static mut T) -> Pin<&'static mut T>iNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Get a pinned mutable reference from a static mutable reference.
This is safe, because T is borrowed for the 'static lifetime, which
never ends.
Trait Implementations
1.2.0 · source impl<'a, T, A> Extend<&'a T> for BTreeSet<T, A> where
T: 'a + Ord + Copy,
A: Allocator + Clone,
impl<'a, T, A> Extend<&'a T> for BTreeSet<T, A> where
T: 'a + Ord + Copy,
A: Allocator + Clone,
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
source fn 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 · source impl<'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,
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
source fn 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.4.0 · source impl<'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,
source fn 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
source fn 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 · source impl<'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,
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
source fn 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 · source impl<'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.
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
source fn 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 · source impl<'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,
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
source fn 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
source impl<G, R> Generator<R> for Pin<&mut G> where
G: Generator<R> + ?Sized,
impl<G, R> Generator<R> for Pin<&mut G> where
G: Generator<R> + ?Sized,
source impl<K, Q, V, A> Index<&Q> for BTreeMap<K, V, A> where
A: Allocator + Clone,
K: Borrow<Q> + Ord,
Q: Ord + ?Sized,
impl<K, Q, V, A> Index<&Q> for BTreeMap<K, V, A> where
A: Allocator + Clone,
K: Borrow<Q> + Ord,
Q: Ord + ?Sized,
source impl<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,
source impl<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]>,
source impl<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,
source fn partial_cmp(&self, other: &&B) -> Option<Ordering>
fn partial_cmp(&self, other: &&B) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
source fn lt(&self, other: &&B) -> bool
fn lt(&self, other: &&B) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
source fn le(&self, other: &&B) -> bool
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
source impl<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,
source fn partial_cmp(&self, other: &&mut B) -> Option<Ordering>
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
source fn lt(&self, other: &&mut B) -> bool
fn lt(&self, other: &&mut B) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
source fn le(&self, other: &&mut B) -> bool
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