Primitive Type tuple
1.0.0 ·Expand description
A finite heterogeneous sequence, (T, U, ..).
Let’s cover each of those in turn:
Tuples are finite. In other words, a tuple has a length. Here’s a tuple
of length 3:
("hello", 5, 'c');Run ‘Length’ is also sometimes called ‘arity’ here; each tuple of a different length is a different, distinct type.
Tuples are heterogeneous. This means that each element of the tuple can have a different type. In that tuple above, it has the type:
(&'static str, i32, char)Run Tuples are a sequence. This means that they can be accessed by position; this is called ‘tuple indexing’, and it looks like this:
let tuple = ("hello", 5, 'c');
assert_eq!(tuple.0, "hello");
assert_eq!(tuple.1, 5);
assert_eq!(tuple.2, 'c');Run The sequential nature of the tuple applies to its implementations of various
traits. For example, in PartialOrd and Ord, the elements are compared
sequentially until the first non-equal set is found.
For more about tuples, see the book.
Trait implementations
In this documentation the shorthand (T1, T2, ..., Tn) is used to represent tuples of varying
length. When that is used, any trait bound expressed on T applies to each element of the
tuple independently. Note that this is a convenience notation to avoid repetitive
documentation, not valid Rust syntax.
Due to a temporary restriction in Rust’s type system, the following traits are only implemented on tuples of arity 12 or less. In the future, this may change:
The following traits are implemented for tuples of any length. These traits have implementations that are automatically generated by the compiler, so are not limited by missing language features.
Examples
Basic usage:
let tuple = ("hello", 5, 'c');
assert_eq!(tuple.0, "hello");Run Tuples are often used as a return type when you want to return more than one value:
fn calculate_point() -> (i32, i32) {
// Don't do a calculation, that's not the point of the example
(4, 5)
}
let point = calculate_point();
assert_eq!(point.0, 4);
assert_eq!(point.1, 5);
// Combining this with patterns can be nicer.
let (x, y) = calculate_point();
assert_eq!(x, 4);
assert_eq!(y, 5);Run Trait Implementations
source impl<T: Clone> Clone for (T1, T2, ..., Tn)
impl<T: Clone> Clone for (T1, T2, ..., Tn)
This trait is implemented on arbitrary-length tuples.
source impl<T> Debug for (T1, T2, ..., Tn) where
T: Debug + ?Sized,
impl<T> Debug for (T1, T2, ..., Tn) where
T: Debug + ?Sized,
This trait is implemented for tuples up to twelve items long.
source impl<T> Default for (T1, T2, ..., Tn) where
T: Default,
impl<T> Default for (T1, T2, ..., Tn) where
T: Default,
This trait is implemented for tuples up to twelve items long.
1.2.0 · source impl<'a, K, V, A> Extend<(&'a K, &'a V)> for BTreeMap<K, V, A> where
K: Ord + Copy,
V: Copy,
A: Allocator + Clone,
impl<'a, K, V, A> Extend<(&'a K, &'a V)> for BTreeMap<K, V, A> where
K: Ord + Copy,
V: Copy,
A: Allocator + Clone,
source fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (&'a K, &'a V)>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (&'a K, &'a V)>,
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, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
source fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
source fn extend_one(&mut self, (k, v): (&'a K, &'a V))
fn extend_one(&mut self, (k, v): (&'a K, &'a V))
Extends a collection with exactly one element.
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.56.0 · source impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB) where
ExtendA: Extend<A>,
ExtendB: Extend<B>,
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB) where
ExtendA: Extend<A>,
ExtendB: Extend<B>,
source fn extend<T>(&mut self, into_iter: T) where
T: IntoIterator<Item = (A, B)>,
fn extend<T>(&mut self, into_iter: T) where
T: IntoIterator<Item = (A, B)>,
Allows to extend a tuple of collections that also implement Extend.
See also: Iterator::unzip
Examples
let mut tuple = (vec![0], vec![1]);
tuple.extend([(2, 3), (4, 5), (6, 7)]);
assert_eq!(tuple.0, [0, 2, 4, 6]);
assert_eq!(tuple.1, [1, 3, 5, 7]);
// also allows for arbitrarily nested tuples as elements
let mut nested_tuple = (vec![1], (vec![2], vec![3]));
nested_tuple.extend([(4, (5, 6)), (7, (8, 9))]);
let (a, (b, c)) = nested_tuple;
assert_eq!(a, [1, 4, 7]);
assert_eq!(b, [2, 5, 8]);
assert_eq!(c, [3, 6, 9]);Run 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<K, V, A> Extend<(K, V)> for BTreeMap<K, V, A> where
K: Ord,
A: Allocator + Clone,
impl<K, V, A> Extend<(K, V)> for BTreeMap<K, V, A> where
K: Ord,
A: Allocator + Clone,
source fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = (K, V)>,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = (K, V)>,
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<K, V, S> Extend<(K, V)> for HashMap<K, V, S> where
K: Eq + Hash,
S: BuildHasher,
impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S> where
K: Eq + Hash,
S: BuildHasher,
Inserts all new key-values from the iterator and replaces values with existing keys with new values returned from the iterator.
source fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: 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.17.0 · source impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
source fn from(pieces: (I, u16)) -> SocketAddr
fn from(pieces: (I, u16)) -> SocketAddr
Converts a tuple struct (Into<IpAddr>, u16) into a SocketAddr.
This conversion creates a SocketAddr::V4 for an IpAddr::V4
and creates a SocketAddr::V6 for an IpAddr::V6.
u16 is treated as port of the newly created SocketAddr.
source impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> where
K: Eq + Hash,
S: BuildHasher + Default,
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> where
K: Eq + Hash,
S: BuildHasher + Default,
source impl<T> Hash for (T1, T2, ..., Tn) where
T: Hash + ?Sized,
impl<T> Hash for (T1, T2, ..., Tn) where
T: Hash + ?Sized,
This trait is implemented for tuples up to twelve items long.
source impl<T> Ord for (T1, T2, ..., Tn) where
T: Ord + ?Sized,
impl<T> Ord for (T1, T2, ..., Tn) where
T: Ord + ?Sized,
This trait is implemented for tuples up to twelve items long.
1.21.0 · source fn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · source fn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · source fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
source impl<T> PartialEq<(T,)> for (T1, T2, ..., Tn) where
T: PartialEq<T> + ?Sized,
impl<T> PartialEq<(T,)> for (T1, T2, ..., Tn) where
T: PartialEq<T> + ?Sized,
This trait is implemented for tuples up to twelve items long.
source impl<T> PartialOrd<(T,)> for (T1, T2, ..., Tn) where
T: PartialOrd<T> + PartialEq<T> + ?Sized,
impl<T> PartialOrd<(T,)> for (T1, T2, ..., Tn) where
T: PartialOrd<T> + PartialEq<T> + ?Sized,
This trait is implemented for tuples up to twelve items long.
source fn partial_cmp(&self, other: &(T,)) -> Option<Ordering>
fn partial_cmp(&self, other: &(T,)) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
source fn lt(&self, other: &(T,)) -> bool
fn lt(&self, other: &(T,)) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
source fn le(&self, other: &(T,)) -> bool
fn le(&self, other: &(T,)) -> bool
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
1.28.0 · source impl<'a, T> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) where
T: 'a + ?Sized,
impl<'a, T> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>) where
T: 'a + ?Sized,
source fn start_bound(&self) -> Bound<&T>
fn start_bound(&self) -> Bound<&T>
Start index bound. Read more
1.35.0 · source fn contains<U>(&self, item: &U) -> bool where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
fn contains<U>(&self, item: &U) -> bool where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
Returns true if item is contained in the range. Read more
1.28.0 · source impl<T> RangeBounds<T> for (Bound<T>, Bound<T>)
impl<T> RangeBounds<T> for (Bound<T>, Bound<T>)
source fn start_bound(&self) -> Bound<&T>
fn start_bound(&self) -> Bound<&T>
Start index bound. Read more
1.35.0 · source fn contains<U>(&self, item: &U) -> bool where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
fn contains<U>(&self, item: &U) -> bool where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
Returns true if item is contained in the range. Read more
1.53.0 · source impl<T> SliceIndex<[T]> for (Bound<usize>, Bound<usize>)
impl<T> SliceIndex<[T]> for (Bound<usize>, Bound<usize>)
source fn get(
self,
slice: &[T]
) -> Option<&<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
fn get(
self,
slice: &[T]
) -> Option<&<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
slice_index_methods)Returns a shared reference to the output at this location, if in bounds. Read more
source fn get_mut(
self,
slice: &mut [T]
) -> Option<&mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
fn get_mut(
self,
slice: &mut [T]
) -> Option<&mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
slice_index_methods)Returns a mutable reference to the output at this location, if in bounds. Read more
source unsafe fn get_unchecked(
self,
slice: *const [T]
) -> *const <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
unsafe fn get_unchecked(
self,
slice: *const [T]
) -> *const <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods)Returns a shared reference to the output at this location, without
performing any bounds checking.
Calling this method with an out-of-bounds index or a dangling slice pointer
is undefined behavior even if the resulting reference is not used. Read more
source unsafe fn get_unchecked_mut(
self,
slice: *mut [T]
) -> *mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
unsafe fn get_unchecked_mut(
self,
slice: *mut [T]
) -> *mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods)Returns a mutable reference to the output at this location, without
performing any bounds checking.
Calling this method with an out-of-bounds index or a dangling slice pointer
is undefined behavior even if the resulting reference is not used. Read more
source impl ToSocketAddrs for (&str, u16)
impl ToSocketAddrs for (&str, u16)
type Iter = IntoIter<SocketAddr, Global>
type Iter = IntoIter<SocketAddr, Global>
Returned iterator over socket addresses which this type may correspond to. Read more
source fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
Converts this object to an iterator of resolved SocketAddrs. Read more
source impl ToSocketAddrs for (IpAddr, u16)
impl ToSocketAddrs for (IpAddr, u16)
type Iter = IntoIter<SocketAddr>
type Iter = IntoIter<SocketAddr>
Returned iterator over socket addresses which this type may correspond to. Read more
source fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
Converts this object to an iterator of resolved SocketAddrs. Read more
source impl ToSocketAddrs for (Ipv4Addr, u16)
impl ToSocketAddrs for (Ipv4Addr, u16)
type Iter = IntoIter<SocketAddr>
type Iter = IntoIter<SocketAddr>
Returned iterator over socket addresses which this type may correspond to. Read more
source fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
Converts this object to an iterator of resolved SocketAddrs. Read more
source impl ToSocketAddrs for (Ipv6Addr, u16)
impl ToSocketAddrs for (Ipv6Addr, u16)
type Iter = IntoIter<SocketAddr>
type Iter = IntoIter<SocketAddr>
Returned iterator over socket addresses which this type may correspond to. Read more
source fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
Converts this object to an iterator of resolved SocketAddrs. Read more
1.46.0 · source impl ToSocketAddrs for (String, u16)
impl ToSocketAddrs for (String, u16)
type Iter = IntoIter<SocketAddr, Global>
type Iter = IntoIter<SocketAddr, Global>
Returned iterator over socket addresses which this type may correspond to. Read more
source fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
Converts this object to an iterator of resolved SocketAddrs. Read more
impl<T: Copy> Copy for (T1, T2, ..., Tn)
This trait is implemented on arbitrary-length tuples.
impl<T> Eq for (T1, T2, ..., Tn) where
T: Eq + ?Sized,
This trait is implemented for tuples up to twelve items long.
Auto Trait Implementations
impl<T> RefUnwindSafe for (T1, T2, ..., Tn) where
T: RefUnwindSafe,
impl<T> Send for (T1, T2, ..., Tn) where
T: Send,
impl<T> Sync for (T1, T2, ..., Tn) where
T: Sync,
impl<T> Unpin for (T1, T2, ..., Tn) where
T: Unpin,
impl<T> UnwindSafe for (T1, T2, ..., Tn) where
T: UnwindSafe,
Blanket Implementations
source impl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · source fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more