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');

‘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§

1.0.0 · Source§

impl<T> Debug for (T₁, T₂, …, Tₙ)
where T: Debug + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
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.

Source§

fn default() -> (T,)

Returns the “default value” for a type. Read more
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,

Source§

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_one(&mut self, _: (&'a K, &'a V))

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
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,

Source§

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))

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements. Read more
1.56.0 · Source§

impl<A, EA> Extend<(A₁, A₂, …, Aₙ)> for (EA₁, EA₂, …, EAₙ)
where EA: Extend<A>,

This trait is implemented for tuples up to twelve items long. The impls for 1- and 3- through 12-ary tuples were stabilized after 2-tuples, in CURRENT_RUSTC_VERSION.

Source§

fn extend<T>(&mut self, into_iter: T)
where T: IntoIterator<Item = (A,)>,

Allows to extend a tuple of collections that also implement Extend.

See also: Iterator::unzip

§Examples
// Example given for a 2-tuple, but 1- through 12-tuples are supported
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]);
Source§

fn extend_one(&mut self, item: (A,))

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements. Read more
1.0.0 · Source§

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)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, _: (K, V))

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements. Read more
1.0.0 · Source§

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)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, (k, v): (K, V))

🔬This is a nightly-only experimental API. (extend_one #72631)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one #72631)
Reserves capacity in a collection for the given number of additional elements. Read more
1.71.0 · Source§

impl<T> From<[T; N]> for (T₁, T₂, …, Tₙ)

This trait is implemented for tuples up to twelve items long.

Source§

fn from(array: [T; 1]) -> (T,)

Converts to this type from the input type.
1.17.0 · Source§

impl<I> From<(I, u16)> for SocketAddr
where I: Into<IpAddr>,

Source§

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.

1.71.0 · Source§

impl<T> From<(T₁, T₂, …, Tₙ)> for [T; N]

This trait is implemented for tuples up to twelve items long.

Source§

fn from(tuple: (T,)) -> [T; 1]

Converts to this type from the input type.
1.79.0 · Source§

impl<A, B, AE, BE> FromIterator<(AE, BE)> for (A, B)
where A: Default + Extend<AE>, B: Default + Extend<BE>,

This implementation turns an iterator of tuples into a tuple of types which implement Default and Extend.

This is similar to Iterator::unzip, but is also composable with other FromIterator implementations:

let string = "1,2,123,4";

let (numbers, lengths): (Vec<_>, Vec<_>) = string
    .split(',')
    .map(|s| s.parse().map(|n: u32| (n, s.len())))
    .collect::<Result<_, _>>()?;

assert_eq!(numbers, [1, 2, 123, 4]);
assert_eq!(lengths, [1, 1, 3, 1]);
Source§

fn from_iter<I>(iter: I) -> (A, B)
where I: IntoIterator<Item = (AE, BE)>,

Creates a value from an iterator. Read more
1.0.0 · Source§

impl<K, V> FromIterator<(K, V)> for BTreeMap<K, V>
where K: Ord,

Source§

fn from_iter<T>(iter: T) -> BTreeMap<K, V>
where T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
1.0.0 · Source§

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S>

Creates a value from an iterator. Read more
1.0.0 · Source§

impl<T> Hash for (T₁, T₂, …, Tₙ)
where T: Hash + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

fn hash<S>(&self, state: &mut S)
where S: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
1.0.0 · Source§

impl<T> Ord for (T₁, T₂, …, Tₙ)
where T: Ord + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

fn cmp(&self, other: &(T,)) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
1.0.0 · Source§

impl<T> PartialEq for (T₁, T₂, …, Tₙ)
where T: PartialEq + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

fn eq(&self, other: &(T,)) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &(T,)) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
1.0.0 · Source§

impl<T> PartialOrd for (T₁, T₂, …, Tₙ)
where T: PartialOrd + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &(T,)) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn ge(&self, other: &(T,)) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

fn gt(&self, other: &(T,)) -> bool

Tests greater than (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,

Source§

fn start_bound(&self) -> Bound<&T>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&T>

End index bound. Read more
1.35.0 · Source§

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>)

Source§

fn start_bound(&self) -> Bound<&T>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&T>

End index bound. Read more
1.35.0 · Source§

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>)

Source§

type Output = [T]

The output type returned by methods.
Source§

fn get( self, slice: &[T], ) -> Option<&<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut( self, slice: &mut [T], ) -> Option<&mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output>

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked( self, slice: *const [T], ) -> *const <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a pointer to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut( self, slice: *mut [T], ) -> *mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable pointer to the output at this location, without performing any bounds checking. Read more
Source§

fn index( self, slice: &[T], ) -> &<(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut( self, slice: &mut [T], ) -> &mut <(Bound<usize>, Bound<usize>) as SliceIndex<[T]>>::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable reference to the output at this location, panicking if out of bounds.
1.73.0 · Source§

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.

Source§

type Output = str

The output type returned by methods.
Source§

fn get(self, slice: &str) -> Option<&str>

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut str) -> Option<&mut str>

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: *const str) -> *const str

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a pointer to the output at this location, without performing any bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut str

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable pointer to the output at this location, without performing any bounds checking. Read more
Source§

fn index(self, slice: &str) -> &str

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut str) -> &mut str

🔬This is a nightly-only experimental API. (slice_index_methods)
Returns a mutable reference to the output at this location, panicking if out of bounds.
1.0.0 · Source§

impl ToSocketAddrs for (&str, u16)

Source§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
Source§

fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>

Converts this object to an iterator of resolved SocketAddrs. Read more
1.0.0 · Source§

impl ToSocketAddrs for (IpAddr, u16)

Source§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
Source§

fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>

Converts this object to an iterator of resolved SocketAddrs. Read more
1.0.0 · Source§

impl ToSocketAddrs for (Ipv4Addr, u16)

Source§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
Source§

fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>

Converts this object to an iterator of resolved SocketAddrs. Read more
1.0.0 · Source§

impl ToSocketAddrs for (Ipv6Addr, u16)

Source§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
Source§

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)

Source§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
Source§

fn to_socket_addrs(&self) -> Result<IntoIter<SocketAddr>>

Converts this object to an iterator of resolved SocketAddrs. Read more
Source§

impl<T> ConstParamTy_ for (T₁, T₂, …, Tₙ)
where T: ConstParamTy_,

This trait is implemented for tuples up to twelve items long.

1.0.0 · Source§

impl<T> Eq for (T₁, T₂, …, Tₙ)
where T: Eq + ?Sized,

This trait is implemented for tuples up to twelve items long.

Source§

impl<T> StructuralPartialEq for (T₁, T₂, …, Tₙ)

This trait is implemented for tuples up to twelve items long.

Source§

impl<T> UnsizedConstParamTy for (T₁, T₂, …, Tₙ)

This trait is implemented for tuples up to twelve items long.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.