Primitive Typetuple
Expand description
A finite heterogeneous sequence,(T, U, ..).
Let’s cover each of those in turn:
Tuples arefinite. In other words, a tuple has a length. Here’s a tupleof length3:
‘Length’ is also sometimes called ‘arity’ here; each tuple of a differentlength is a different, distinct type.
Tuples areheterogeneous. This means that each element of the tuple canhave a different type. In that tuple above, it has the type:
Tuples are asequence. This means that they can be accessed by position;this is called ‘tuple indexing’, and it looks like this:
lettuple = ("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 varioustraits. For example, inPartialOrd andOrd, the elements are comparedsequentially until the first non-equal set is found.
For more about tuples, seethe book.
§Trait implementations
In this documentation the shorthand(T₁, T₂, …, Tₙ) is used to represent tuples of varyinglength. When that is used, any trait bound expressed onT applies to each element of thetuple independently. Note that this is a convenience notation to avoid repetitivedocumentation, not valid Rust syntax.
Due to a temporary restriction in Rust’s type system, the following traits are onlyimplemented 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 haveimplementations that are automatically generated by the compiler, so are not limited bymissing language features.
§Examples
Basic usage:
Tuples are often used as a return type when you want to return more thanone value:
fncalculate_point() -> (i32, i32) {// Don't do a calculation, that's not the point of the example(4,5)}letpoint = 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:
Trait Implementations§
1.0.0 ·Source§impl<T>Debug for(T₁, T₂, …, Tₙ)where T:Debug,
This trait is implemented for tuples up to twelve items long.
impl<T>Debug for(T₁, T₂, …, Tₙ)where T:Debug,
This trait is implemented for tuples up to twelve items long.
1.0.0 ·Source§impl<T>Default for(T₁, T₂, …, Tₙ)where T:Default,
This trait is implemented for tuples up to twelve items long.
impl<T>Default for(T₁, T₂, …, Tₙ)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)> forBTreeMap<K, V, A>
impl<'a, K, V, A>Extend<(&'a K,&'a V)> forBTreeMap<K, V, A>
1.4.0 ·Source§impl<'a, K, V, S>Extend<(&'a K,&'a V)> forHashMap<K, V, S>
impl<'a, K, V, S>Extend<(&'a K,&'a V)> forHashMap<K, V, S>
1.0.0 ·Source§impl<K, V, A>Extend<(K, V)> forBTreeMap<K, V, A>
impl<K, V, A>Extend<(K, V)> forBTreeMap<K, V, A>
1.0.0 ·Source§impl<K, V, S>Extend<(K, V)> forHashMap<K, V, S>
Inserts all new key-values from the iterator and replaces values with existingkeys with new values returned from the iterator.
impl<K, V, S>Extend<(K, V)> forHashMap<K, V, S>
Inserts all new key-values from the iterator and replaces values with existingkeys with new values returned from the iterator.
1.56.0 ·Source§impl<T, ExtendT>Extend<(T₁, T₂, …, Tₙ)> for(ExtendT₁, ExtendT₂, …, ExtendTₙ)where ExtendT:Extend<T>,
This trait is implemented for tuples up to twelve items long. Theimpls for1- and 3- through 12-ary tuples were stabilized after 2-tuples, in 1.85.0.
impl<T, ExtendT>Extend<(T₁, T₂, …, Tₙ)> for(ExtendT₁, ExtendT₂, …, ExtendTₙ)where ExtendT:Extend<T>,
This trait is implemented for tuples up to twelve items long. Theimpls for1- and 3- through 12-ary tuples were stabilized after 2-tuples, in 1.85.0.
Source§fnextend<I>(&mut self, iter: I)where I:IntoIterator<Item =(T,)>,
fnextend<I>(&mut self, iter: I)where I:IntoIterator<Item =(T,)>,
Allows toextend a tuple of collections that also implementExtend.
See also:Iterator::unzip
§Examples
// Example given for a 2-tuple, but 1- through 12-tuples are supportedletmuttuple = (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 elementsletmutnested_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]);Source§fnextend_one(&mut self, item:(T,))
fnextend_one(&mut self, item:(T,))
extend_one #72631)1.71.0 ·Source§impl<T>From<[T; N]> for(T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
impl<T>From<[T; N]> for(T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
1.17.0 (const:unstable) ·Source§impl<I>From<(I,u16)> forSocketAddr
impl<I>From<(I,u16)> forSocketAddr
Source§fnfrom(pieces: (I,u16)) ->SocketAddr
fnfrom(pieces: (I,u16)) ->SocketAddr
Converts a tuple struct (Into<IpAddr>,u16) into aSocketAddr.
This conversion creates aSocketAddr::V4 for anIpAddr::V4and creates aSocketAddr::V6 for anIpAddr::V6.
u16 is treated as port of the newly createdSocketAddr.
1.71.0 ·Source§impl<T>From<(T₁, T₂, …, Tₙ)> for[T; N]
This trait is implemented for tuples up to twelve items long.
impl<T>From<(T₁, T₂, …, Tₙ)> for[T; N]
This trait is implemented for tuples up to twelve items long.
1.0.0 ·Source§impl<K, V, S>FromIterator<(K, V)> forHashMap<K, V, S>
impl<K, V, S>FromIterator<(K, V)> forHashMap<K, V, S>
1.79.0 ·Source§impl<T, ExtendT>FromIterator<(T₁, T₂, …, Tₙ)> for(ExtendT₁, ExtendT₂, …, ExtendTₙ)
impl<T, ExtendT>FromIterator<(T₁, T₂, …, Tₙ)> for(ExtendT₁, ExtendT₂, …, ExtendTₙ)
This is similar toIterator::unzip, but is also composable with otherFromIteratorimplementations:
Source§fnfrom_iter<Iter>(iter: Iter) ->(ExtendT,)where Iter:IntoIterator<Item =(T,)>,
fnfrom_iter<Iter>(iter: Iter) ->(ExtendT,)where Iter:IntoIterator<Item =(T,)>,
1.0.0 ·Source§impl<T>Hash for(T₁, T₂, …, Tₙ)where T:Hash,
This trait is implemented for tuples up to twelve items long.
impl<T>Hash for(T₁, T₂, …, Tₙ)where T:Hash,
This trait is implemented for tuples up to twelve items long.
Source§impl<T>IntoBounds<T> for (Bound<T>,Bound<T>)
impl<T>IntoBounds<T> for (Bound<T>,Bound<T>)
1.0.0 (const:unstable) ·Source§impl<T>Ord for(T₁, T₂, …, Tₙ)where T:Ord,
This trait is implemented for tuples up to twelve items long.
impl<T>Ord for(T₁, T₂, …, Tₙ)where T:Ord,
This trait is implemented for tuples up to twelve items long.
1.0.0 (const:unstable) ·Source§impl<T>PartialEq for(T₁, T₂, …, Tₙ)where T:PartialEq,
This trait is implemented for tuples up to twelve items long.
impl<T>PartialEq for(T₁, T₂, …, Tₙ)where T:PartialEq,
This trait is implemented for tuples up to twelve items long.
1.0.0 (const:unstable) ·Source§impl<T>PartialOrd for(T₁, T₂, …, Tₙ)where T:PartialOrd,
This trait is implemented for tuples up to twelve items long.
impl<T>PartialOrd for(T₁, T₂, …, Tₙ)where T:PartialOrd,
This trait is implemented for tuples up to twelve items long.
1.28.0 (const: unstable) ·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,
1.28.0 (const: unstable) ·Source§impl<T>RangeBounds<T> for (Bound<T>,Bound<T>)
impl<T>RangeBounds<T> for (Bound<T>,Bound<T>)
1.53.0 ·Source§impl<T>SliceIndex<[T]> for (Bound<usize>,Bound<usize>)
impl<T>SliceIndex<[T]> for (Bound<usize>,Bound<usize>)
Source§fnget( self, slice: &[T],) ->Option<&<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output>
fnget( self, slice: &[T],) ->Option<&<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output>
slice_index_methods)Source§fnget_mut( self, slice: &mut[T],) ->Option<&mut <(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output>
fnget_mut( self, slice: &mut[T],) ->Option<&mut <(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output>
slice_index_methods)Source§unsafe fnget_unchecked( self, slice:*const[T],) ->*const<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output
unsafe fnget_unchecked( self, slice:*const[T],) ->*const<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output
slice_index_methods)Source§unsafe fnget_unchecked_mut( self, slice:*mut[T],) ->*mut<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output
unsafe fnget_unchecked_mut( self, slice:*mut[T],) ->*mut<(Bound<usize>,Bound<usize>) asSliceIndex<[T]>>::Output
slice_index_methods)Source§implSliceIndex<ByteStr> for (Bound<usize>,Bound<usize>)
implSliceIndex<ByteStr> for (Bound<usize>,Bound<usize>)
Source§fnget( self, slice: &ByteStr,) ->Option<&<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output>
fnget( self, slice: &ByteStr,) ->Option<&<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output>
slice_index_methods)Source§fnget_mut( self, slice: &mutByteStr,) ->Option<&mut <(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output>
fnget_mut( self, slice: &mutByteStr,) ->Option<&mut <(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output>
slice_index_methods)Source§unsafe fnget_unchecked( self, slice:*constByteStr,) ->*const<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output
unsafe fnget_unchecked( self, slice:*constByteStr,) ->*const<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output
slice_index_methods)Source§unsafe fnget_unchecked_mut( self, slice:*mutByteStr,) ->*mut<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output
unsafe fnget_unchecked_mut( self, slice:*mutByteStr,) ->*mut<(Bound<usize>,Bound<usize>) asSliceIndex<ByteStr>>::Output
slice_index_methods)1.73.0 ·Source§implSliceIndex<str> for (Bound<usize>,Bound<usize>)
Implements substring slicing for arbitrary bounds.
implSliceIndex<str> for (Bound<usize>,Bound<usize>)
Implements substring slicing for arbitrary bounds.
Returns a slice of the given string bounded by the byte indicesprovided by each bound.
This operation isO(1).
§Panics
Panics ifbegin orend (if it exists and once adjusted forinclusion/exclusion) does not point to the starting byte offset ofa character (as defined byis_char_boundary), ifbegin > end, or ifend > len.
Source§fnget(self, slice: &str) ->Option<&str>
fnget(self, slice: &str) ->Option<&str>
slice_index_methods)Source§fnget_mut(self, slice: &mutstr) ->Option<&mutstr>
fnget_mut(self, slice: &mutstr) ->Option<&mutstr>
slice_index_methods)Source§unsafe fnget_unchecked(self, slice:*conststr) ->*conststr
unsafe fnget_unchecked(self, slice:*conststr) ->*conststr
slice_index_methods)Source§unsafe fnget_unchecked_mut(self, slice:*mutstr) ->*mutstr
unsafe fnget_unchecked_mut(self, slice:*mutstr) ->*mutstr
slice_index_methods)1.0.0 ·Source§implToSocketAddrs for (&str,u16)
implToSocketAddrs for (&str,u16)
Source§typeIter =IntoIter<SocketAddr>
typeIter =IntoIter<SocketAddr>
Source§fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
SocketAddrs.Read more1.0.0 ·Source§implToSocketAddrs for (IpAddr,u16)
implToSocketAddrs for (IpAddr,u16)
Source§typeIter =IntoIter<SocketAddr>
typeIter =IntoIter<SocketAddr>
Source§fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
SocketAddrs.Read more1.0.0 ·Source§implToSocketAddrs for (Ipv4Addr,u16)
implToSocketAddrs for (Ipv4Addr,u16)
Source§typeIter =IntoIter<SocketAddr>
typeIter =IntoIter<SocketAddr>
Source§fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
SocketAddrs.Read more1.0.0 ·Source§implToSocketAddrs for (Ipv6Addr,u16)
implToSocketAddrs for (Ipv6Addr,u16)
Source§typeIter =IntoIter<SocketAddr>
typeIter =IntoIter<SocketAddr>
Source§fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
SocketAddrs.Read more1.46.0 ·Source§implToSocketAddrs for (String,u16)
implToSocketAddrs for (String,u16)
Source§typeIter =IntoIter<SocketAddr>
typeIter =IntoIter<SocketAddr>
Source§fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
fnto_socket_addrs(&self) ->Result<IntoIter<SocketAddr>>
SocketAddrs.Read moreimpl<T>CloneFromCell for(T₁, T₂, …, Tₙ)where T:CloneFromCell,
This trait is implemented for tuples up to twelve items long.
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,
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.