On this page
Primitive Type tuple
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');
‘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)
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');
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 (T₁, T₂, …, Tₙ)
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");
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);
Homogeneous tuples can be created from arrays of appropriate length:
let array: [u32; 3] = [1, 2, 3];
let tuple: (u32, u32, u32) = array.into();
Trait Implementations
impl<T> Debug for (T₁, T₂, …, Tₙ)
where
T: Debug + ?Sized,
This trait is implemented for tuples up to twelve items long.
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl<T> Default for (T₁, T₂, …, Tₙ)
where
T: Default,
This trait is implemented for tuples up to twelve items long.
impl<'a, K, V, A> Extend<(&'a K, &'a V)> for BTreeMap<K, V, A>
where
K: Ord + Copy,
V: Copy,
A: Allocator + Clone,
fn extend<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (&'a K, &'a V)>,
fn extend_one(&mut self, _: (&'a K, &'a V))
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
where
K: Eq + Hash + Copy,
V: Copy,
S: BuildHasher,
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend_one(&mut self, (k, v): (&'a K, &'a V))
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
where
ExtendA: Extend<A>,
ExtendB: Extend<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]);
fn extend_one(&mut self, item: (A, B))
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
impl<K, V, A> Extend<(K, V)> for BTreeMap<K, V, A>
where
K: Ord,
A: Allocator + Clone,
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = (K, V)>,
fn extend_one(&mut self, _: (K, V))
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
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.
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend_one(&mut self, (k, v): (K, V))
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
impl<T> From<[T; 1]> for (T,)
fn from(array: [T; 1]) -> (T,)
impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)
fn from(array: [T; 10]) -> (T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)
fn from(array: [T; 11]) -> (T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)
fn from(array: [T; 12]) -> (T, T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 2]> for (T, T)
fn from(array: [T; 2]) -> (T, T)
impl<T> From<[T; 3]> for (T, T, T)
fn from(array: [T; 3]) -> (T, T, T)
impl<T> From<[T; 4]> for (T, T, T, T)
fn from(array: [T; 4]) -> (T, T, T, T)
impl<T> From<[T; 5]> for (T, T, T, T, T)
fn from(array: [T; 5]) -> (T, T, T, T, T)
impl<T> From<[T; 6]> for (T, T, T, T, T, T)
fn from(array: [T; 6]) -> (T, T, T, T, T, T)
impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)
fn from(array: [T; 7]) -> (T, T, T, T, T, T, T)
impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)
fn from(array: [T; 8]) -> (T, T, T, T, T, T, T, T)
impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)
fn from(array: [T; 9]) -> (T, T, T, T, T, T, T, T, T)
impl<I> From<(I, u16)> for SocketAddr
where
I: Into<IpAddr>,
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
.
impl<T> From<(T,)> for [T; 1]
fn from(tuple: (T,)) -> [T; 1]
impl<T> From<(T, T)> for [T; 2]
fn from(tuple: (T, T)) -> [T; 2]
impl<T> From<(T, T, T)> for [T; 3]
fn from(tuple: (T, T, T)) -> [T; 3]
impl<T> From<(T, T, T, T)> for [T; 4]
fn from(tuple: (T, T, T, T)) -> [T; 4]
impl<T> From<(T, T, T, T, T)> for [T; 5]
fn from(tuple: (T, T, T, T, T)) -> [T; 5]
impl<T> From<(T, T, T, T, T, T)> for [T; 6]
fn from(tuple: (T, T, T, T, T, T)) -> [T; 6]
impl<T> From<(T, T, T, T, T, T, T)> for [T; 7]
fn from(tuple: (T, T, T, T, T, T, T)) -> [T; 7]
impl<T> From<(T, T, T, T, T, T, T, T)> for [T; 8]
fn from(tuple: (T, T, T, T, T, T, T, T)) -> [T; 8]
impl<T> From<(T, T, T, T, T, T, T, T, T)> for [T; 9]
fn from(tuple: (T, T, T, T, T, T, T, T, T)) -> [T; 9]
impl<T> From<(T, T, T, T, T, T, T, T, T, T)> for [T; 10]
fn from(tuple: (T, T, T, T, T, T, T, T, T, T)) -> [T; 10]
impl<T> From<(T, T, T, T, T, T, T, T, T, T, T)> for [T; 11]
fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T)) -> [T; 11]
impl<T> From<(T, T, T, T, T, T, T, T, T, T, T, T)> for [T; 12]
fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T, T)) -> [T; 12]
impl<K, V> FromIterator<(K, V)> for BTreeMap<K, V>
where
K: Ord,
fn from_iter<T>(iter: T) -> BTreeMap<K, V>
where
T: IntoIterator<Item = (K, V)>,
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S>
impl<T> Hash for (T₁, T₂, …, Tₙ)
where
T: Hash + ?Sized,
This trait is implemented for tuples up to twelve items long.
fn hash<S>(&self, state: &mut S)
where
S: Hasher,
impl<T> Ord for (T₁, T₂, …, Tₙ)
where
T: Ord + ?Sized,
This trait is implemented for tuples up to twelve items long.
fn cmp(&self, other: &(T,)) -> Ordering
impl<T> PartialEq for (T₁, T₂, …, Tₙ)
where
T: PartialEq + ?Sized,
This trait is implemented for tuples up to twelve items long.
fn eq(&self, other: &(T,)) -> bool
self
and other
values to be equal, and is used by ==
.
fn ne(&self, other: &(T,)) -> bool
!=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
impl<T> PartialOrd for (T₁, T₂, …, Tₙ)
where
T: PartialOrd + ?Sized,
This trait is implemented for tuples up to twelve items long.
fn partial_cmp(&self, other: &(T,)) -> Option<Ordering>
fn lt(&self, other: &(T,)) -> bool
fn le(&self, other: &(T,)) -> bool
self
and other
) and is used by the <=
operator. Read more
fn ge(&self, other: &(T,)) -> bool
self
and other
) and is used by the >=
operator. Read more
fn gt(&self, other: &(T,)) -> bool
impl<'a, T> RangeBounds<T> for (Bound<&'a T>, Bound<&'a T>)
where
T: 'a + ?Sized,
fn start_bound(&self) -> Bound<&T>
fn end_bound(&self) -> Bound<&T>
fn contains<U>(&self, item: &U) -> bool
where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
impl<T> RangeBounds<T> for (Bound<T>, Bound<T>)
fn start_bound(&self) -> Bound<&T>
fn end_bound(&self) -> Bound<&T>
fn contains<U>(&self, item: &U) -> bool
where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,
impl<T> SliceIndex<[T]> for (Bound<usize>, Bound<usize>)
type Output = [T]
fn get(
self,
slice: &[T]
) -> Option<&<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
slice_index_methods
)
fn get_mut(
self,
slice: &mut [T]
) -> Option<&mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>
slice_index_methods
)
unsafe fn get_unchecked(
self,
slice: *const [T]
) -> *const <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods
)
slice
pointer is undefined behavior even if the resulting reference is not used.
unsafe fn get_unchecked_mut(
self,
slice: *mut [T]
) -> *mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods
)
slice
pointer is undefined behavior even if the resulting reference is not used.
fn index(
self,
slice: &[T]
) -> &<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods
)
fn index_mut(
self,
slice: &mut [T]
) -> &mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output
slice_index_methods
)
impl SliceIndex<str> for (Bound<usize>, Bound<usize>)
Implements substring slicing for arbitrary bounds.
Returns a slice of the given string bounded by the byte indices provided by each bound.
This operation is O(1).
Panics
Panics if begin
or end
(if it exists and once adjusted for inclusion/exclusion) does not point to the starting byte offset of a character (as defined by is_char_boundary
), if begin > end
, or if end > len
.
type Output = str
fn get(self, slice: &str) -> Option<&str>
slice_index_methods
)
fn get_mut(self, slice: &mut str) -> Option<&mut str>
slice_index_methods
)
unsafe fn get_unchecked(self, slice: *const str) -> *const str
slice_index_methods
)
slice
pointer is undefined behavior even if the resulting reference is not used.
unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut str
slice_index_methods
)
slice
pointer is undefined behavior even if the resulting reference is not used.
fn index(self, slice: &str) -> &str
slice_index_methods
)
fn index_mut(self, slice: &mut str) -> &mut str
slice_index_methods
)
impl ToSocketAddrs for (&str, u16)
type Iter = IntoIter<SocketAddr>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
SocketAddr
s. Read more
impl ToSocketAddrs for (IpAddr, u16)
type Iter = IntoIter<SocketAddr>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
SocketAddr
s. Read more
impl ToSocketAddrs for (Ipv4Addr, u16)
type Iter = IntoIter<SocketAddr>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
SocketAddr
s. Read more
impl ToSocketAddrs for (Ipv6Addr, u16)
type Iter = IntoIter<SocketAddr>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
SocketAddr
s. Read more
impl ToSocketAddrs for (String, u16)
type Iter = IntoIter<SocketAddr>
fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>
SocketAddr
s. Read more
impl<T> ConstParamTy for (T₁, T₂, …, Tₙ)
where
T: ConstParamTy,
This trait is implemented for tuples up to twelve items long.
impl<T> Eq for (T₁, T₂, …, Tₙ)
where
T: Eq + ?Sized,
This trait is implemented for tuples up to twelve items long.
impl<T> StructuralEq for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
impl<T> StructuralPartialEq for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
Auto Trait Implementations
impl<T> RefUnwindSafe for (T₁, T₂, …, Tₙ)
where
T: RefUnwindSafe,
impl<T> Send for (T₁, T₂, …, Tₙ)
where
T: Send,
impl<T> Sync for (T₁, T₂, …, Tₙ)
where
T: Sync,
impl<T> Unpin for (T₁, T₂, …, Tₙ)
where
T: Unpin,
impl<T> UnwindSafe for (T₁, T₂, …, Tₙ)
where
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T
where
T: 'static + ?Sized,
impl<T> Borrow<T> for T
where
T: ?Sized,
impl<T> BorrowMut<T> for T
where
T: ?Sized,
impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for T
where
U: From<T>,
fn into(self) -> U
Calls U::from(self)
.
That is, this conversion is whatever the implementation of From<T> for U
chooses to do.
impl<T> ToOwned for T
where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)
impl<T, U> TryFrom<U> for T
where
U: Into<T>,
type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/primitive.tuple.html