Primitive Type reference1.0.0 [−]
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.
For more information on how to use references, see the book's section on "References and Borrowing".
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
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
exceptPointer
andfmt::Write
PartialOrd
Ord
PartialEq
Eq
AsRef
Fn
(in addition,&T
references getFnMut
andFnOnce
ifT: Fn
)Hash
ToSocketAddrs
&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
Send
(note that&T
references only getSend
ifT: Sync
)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
impl<'a, A, F> FnOnce<A> for &'a mut F where
F: FnMut<A> + ?Sized,
[src]
[+]
impl<'a, A, F> FnOnce<A> for &'a mut F where
F: FnMut<A> + ?Sized,
impl<'a, A, F> FnOnce<A> for &'a F where
F: Fn<A> + ?Sized,
[src]
[+]
impl<'a, A, F> FnOnce<A> for &'a F where
F: Fn<A> + ?Sized,
impl<'a, I> DoubleEndedIterator for &'a mut I where
I: DoubleEndedIterator + ?Sized,
[src]
[+]
impl<'a, I> DoubleEndedIterator for &'a mut I where
I: DoubleEndedIterator + ?Sized,
impl<'a, T> DerefMut for &'a mut T where
T: ?Sized,
[src]
[+]
impl<'a, T> DerefMut for &'a mut T where
T: ?Sized,
impl<'a, T> Pointer for &'a T where
T: ?Sized,
[src]
[+]
impl<'a, T> Pointer for &'a T where
T: ?Sized,
impl<'a, T> Pointer for &'a mut T where
T: ?Sized,
[src]
[+]
impl<'a, T> Pointer for &'a mut T where
T: ?Sized,
impl<'a, T> Binary for &'a mut T where
T: Binary + ?Sized,
[src]
[+]
impl<'a, T> Binary for &'a mut T where
T: Binary + ?Sized,
impl<'a, T> Binary for &'a T where
T: Binary + ?Sized,
[src]
[+]
impl<'a, T> Binary for &'a T where
T: Binary + ?Sized,
impl<'a, T> Debug for &'a mut T where
T: Debug + ?Sized,
[src]
[+]
impl<'a, T> Debug for &'a mut T where
T: Debug + ?Sized,
impl<'a, T> Debug for &'a T where
T: Debug + ?Sized,
[src]
[+]
impl<'a, T> Debug for &'a T where
T: Debug + ?Sized,
impl<'a, I> ExactSizeIterator for &'a mut I where
I: ExactSizeIterator + ?Sized,
[src]
[+]
impl<'a, I> ExactSizeIterator for &'a mut I where
I: ExactSizeIterator + ?Sized,
impl<'a, W> Write for &'a mut W where
W: Write + ?Sized,
1.4.0[src]
[+]
impl<'a, W> Write for &'a mut W where
W: Write + ?Sized,
impl<'a, T> Send for &'a T where
T: Sync + ?Sized,
[src]
impl<'a, T> Send for &'a T where
T: Sync + ?Sized,
impl<'a, T> Send for &'a mut T where
T: Send + ?Sized,
[src]
impl<'a, T> Send for &'a mut T where
T: Send + ?Sized,
impl<'a, I> FusedIterator for &'a mut I where
I: FusedIterator + ?Sized,
1.26.0[src]
impl<'a, I> FusedIterator for &'a mut I where
I: FusedIterator + ?Sized,
impl<'a, A> Ord for &'a A where
A: Ord + ?Sized,
[src]
[+]
impl<'a, A> Ord for &'a A where
A: Ord + ?Sized,
impl<'a, A> Ord for &'a mut A where
A: Ord + ?Sized,
[src]
[+]
impl<'a, A> Ord for &'a mut A where
A: Ord + ?Sized,
impl<'a, T> Hash for &'a T where
T: Hash + ?Sized,
[src]
[+]
impl<'a, T> Hash for &'a T where
T: Hash + ?Sized,
impl<'a, T> Hash for &'a mut T where
T: Hash + ?Sized,
[src]
[+]
impl<'a, T> Hash for &'a mut T where
T: Hash + ?Sized,
impl<'a, 'b, A, B> PartialEq<&'b 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,
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 A where
A: PartialEq<B> + ?Sized,
B: ?Sized,
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 B> for &'a A where
A: PartialEq<B> + ?Sized,
B: ?Sized,
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 mut B> for &'a mut A where
A: PartialEq<B> + ?Sized,
B: ?Sized,
impl<'a, A, F> Fn<A> for &'a F where
F: Fn<A> + ?Sized,
[src]
[+]
impl<'a, A, F> Fn<A> for &'a F where
F: Fn<A> + ?Sized,
impl<'a, T, U> AsRef<U> for &'a mut T where
T: AsRef<U> + ?Sized,
U: ?Sized,
[src]
[+]
impl<'a, T, U> AsRef<U> for &'a mut T where
T: AsRef<U> + ?Sized,
U: ?Sized,
impl<'a, T, U> AsRef<U> for &'a T where
T: AsRef<U> + ?Sized,
U: ?Sized,
[src]
[+]
impl<'a, T, U> AsRef<U> for &'a T where
T: AsRef<U> + ?Sized,
U: ?Sized,
impl<'a, T> LowerExp for &'a mut T where
T: LowerExp + ?Sized,
[src]
[+]
impl<'a, T> LowerExp for &'a mut T where
T: LowerExp + ?Sized,
impl<'a, T> LowerExp for &'a T where
T: LowerExp + ?Sized,
[src]
[+]
impl<'a, T> LowerExp for &'a T where
T: LowerExp + ?Sized,
impl<'a, T> LowerHex for &'a T where
T: LowerHex + ?Sized,
[src]
[+]
impl<'a, T> LowerHex for &'a T where
T: LowerHex + ?Sized,
impl<'a, T> LowerHex for &'a mut T where
T: LowerHex + ?Sized,
[src]
[+]
impl<'a, T> LowerHex for &'a mut T where
T: LowerHex + ?Sized,
impl<'a, T> Display for &'a mut T where
T: Display + ?Sized,
[src]
[+]
impl<'a, T> Display for &'a mut T where
T: Display + ?Sized,
impl<'a, T> Display for &'a T where
T: Display + ?Sized,
[src]
[+]
impl<'a, T> Display for &'a T where
T: Display + ?Sized,
impl<'a, T> Borrow<T> for &'a T where
T: ?Sized,
[src]
[+]
impl<'a, T> Borrow<T> for &'a T where
T: ?Sized,
impl<'a, T> Borrow<T> for &'a mut T where
T: ?Sized,
[src]
[+]
impl<'a, T> Borrow<T> for &'a mut T where
T: ?Sized,
impl<'a, I> Iterator for &'a mut I where
I: Iterator + ?Sized,
[src]
[+]
impl<'a, I> Iterator for &'a mut I where
I: Iterator + ?Sized,
impl<'a, T> Deref for &'a mut T where
T: ?Sized,
[src]
[+]
impl<'a, T> Deref for &'a mut T where
T: ?Sized,
impl<'a, T> Deref for &'a T where
T: ?Sized,
[src]
[+]
impl<'a, T> Deref for &'a T where
T: ?Sized,
impl<'a, I> TrustedLen for &'a mut I where
I: TrustedLen + ?Sized,
[src]
impl<'a, I> TrustedLen for &'a mut I where
I: TrustedLen + ?Sized,
impl<'a, A, F> FnMut<A> for &'a mut F where
F: FnMut<A> + ?Sized,
[src]
[+]
impl<'a, A, F> FnMut<A> for &'a mut F where
F: FnMut<A> + ?Sized,
impl<'a, A, F> FnMut<A> for &'a F where
F: Fn<A> + ?Sized,
[src]
[+]
impl<'a, A, F> FnMut<A> for &'a F where
F: Fn<A> + ?Sized,
impl<'a, T, U> AsMut<U> for &'a mut T where
T: AsMut<U> + ?Sized,
U: ?Sized,
[src]
[+]
impl<'a, T, U> AsMut<U> for &'a mut T where
T: AsMut<U> + ?Sized,
U: ?Sized,
impl<'a, T> UpperExp for &'a T where
T: UpperExp + ?Sized,
[src]
[+]
impl<'a, T> UpperExp for &'a T where
T: UpperExp + ?Sized,
impl<'a, T> UpperExp for &'a mut T where
T: UpperExp + ?Sized,
[src]
[+]
impl<'a, T> UpperExp for &'a mut T where
T: UpperExp + ?Sized,
impl<'a, T> UpperHex for &'a mut T where
T: UpperHex + ?Sized,
[src]
[+]
impl<'a, T> UpperHex for &'a mut T where
T: UpperHex + ?Sized,
impl<'a, T> UpperHex for &'a T where
T: UpperHex + ?Sized,
[src]
[+]
impl<'a, T> UpperHex for &'a T where
T: UpperHex + ?Sized,
impl<'a, 'b, A, B> PartialOrd<&'b mut B> for &'a mut 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,
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 B> for &'a A where
A: PartialOrd<B> + ?Sized,
B: ?Sized,
impl<'a, T> Octal for &'a mut T where
T: Octal + ?Sized,
[src]
[+]
impl<'a, T> Octal for &'a mut T where
T: Octal + ?Sized,
impl<'a, T> Octal for &'a T where
T: Octal + ?Sized,
[src]
[+]
impl<'a, T> Octal for &'a T where
T: Octal + ?Sized,
impl<'a, T> BorrowMut<T> for &'a mut T where
T: ?Sized,
[src]
[+]
impl<'a, T> BorrowMut<T> for &'a mut T where
T: ?Sized,
impl<'a, T> Generator for &'a mut T where
T: Generator + ?Sized,
[src]
[+]
impl<'a, T> Generator for &'a mut T where
T: Generator + ?Sized,
impl<'a, H> Hasher for &'a mut H where
H: Hasher + ?Sized,
1.22.0[src]
[+]
impl<'a, H> Hasher for &'a mut H where
H: Hasher + ?Sized,
impl<'a, A> Eq for &'a A where
A: Eq + ?Sized,
[src]
impl<'a, A> Eq for &'a A where
A: Eq + ?Sized,
impl<'a, A> Eq for &'a mut A where
A: Eq + ?Sized,
[src]
impl<'a, A> Eq for &'a mut A where
A: Eq + ?Sized,
impl<'a, T, U> CoerceUnsized<*const U> for &'a T where
T: Unsize<U> + ?Sized,
U: ?Sized,
[src]
impl<'a, T, U> CoerceUnsized<*const U> for &'a T where
T: Unsize<U> + ?Sized,
U: ?Sized,
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<&'a mut U> for &'a mut T where
T: Unsize<U> + ?Sized,
U: ?Sized,
impl<'a, T, U> CoerceUnsized<*mut U> for &'a mut 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,
impl<'a, T, U> CoerceUnsized<*const 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,
impl<'a, 'b, T, U> CoerceUnsized<&'a U> for &'b 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,
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 mut T where
'b: 'a,
T: Unsize<U> + ?Sized,
U: ?Sized,
impl<'a, R: Read + ?Sized> Read for &'a mut R
[src]
[+]
impl<'a, R: Read + ?Sized> Read for &'a mut R
impl<'a, W: Write + ?Sized> Write for &'a mut W
[src]
[+]
impl<'a, W: Write + ?Sized> Write for &'a mut W
impl<'a, S: Seek + ?Sized> Seek for &'a mut S
[src]
[+]
impl<'a, S: Seek + ?Sized> Seek for &'a mut S
impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B
[src]
[+]
impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B
impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T
[src]
[+]
impl<'a, T: ToSocketAddrs + ?Sized> ToSocketAddrs for &'a T
impl<'a, T: ?Sized> !UnwindSafe for &'a mut T
1.9.0[src]
impl<'a, T: ?Sized> !UnwindSafe for &'a mut T
impl<'a, T: RefUnwindSafe + ?Sized> UnwindSafe for &'a T
1.9.0[src]
impl<'a, T: RefUnwindSafe + ?Sized> UnwindSafe for &'a T