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 (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");
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

Homogeneous tuples can be created from arrays of appropriate length:

let array: [u32; 3] = [1, 2, 3];
let tuple: (u32, u32, u32) = array.into();
Run

Trait Implementations§

source§

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

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

source§

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

Formats the value using the given formatter. Read more
source§

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

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.56.0 · source§

impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)
where ExtendA: Extend<A>, ExtendB: Extend<B>,

source§

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

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_one(&mut self, item: (A, B))

🔬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; 1]> for (T,)

source§

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

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)

source§

fn from(array: [T; 10]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)

source§

fn from(array: [T; 11]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)

source§

fn from(array: [T; 12]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 2]> for (T, T)

source§

fn from(array: [T; 2]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 3]> for (T, T, T)

source§

fn from(array: [T; 3]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 4]> for (T, T, T, T)

source§

fn from(array: [T; 4]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 5]> for (T, T, T, T, T)

source§

fn from(array: [T; 5]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 6]> for (T, T, T, T, T, T)

source§

fn from(array: [T; 6]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)

source§

fn from(array: [T; 7]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)

source§

fn from(array: [T; 8]) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)

source§

fn from(array: [T; 9]) -> Self

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

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

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,)> for [T; 1]

source§

fn from(tuple: (T,)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T)> for [T; 2]

source§

fn from(tuple: (T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T)> for [T; 3]

source§

fn from(tuple: (T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T)> for [T; 4]

source§

fn from(tuple: (T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T)> for [T; 5]

source§

fn from(tuple: (T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T)> for [T; 6]

source§

fn from(tuple: (T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T)> for [T; 7]

source§

fn from(tuple: (T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T)> for [T; 8]

source§

fn from(tuple: (T, T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T)> for [T; 9]

source§

fn from(tuple: (T, T, T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T)> for [T; 10]

source§

fn from(tuple: (T, T, T, T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T, T)> for [T; 11]

source§

fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
1.71.0 · source§

impl<T> From<(T, T, T, T, T, T, T, T, T, T, T, T)> for [T; 12]

source§

fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T, T)) -> Self

Converts to this type from the input type.
source§

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

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

source§

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

Feeds this value into the given Hasher. Read more
source§

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

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

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

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

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

This method tests less than (for self and other) and is used by the < operator. Read more
source§

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

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

This method 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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.28.0 · source§

impl<'a, T: ?Sized + 'a> RangeBounds<T> for (Bound<&'a T>, Bound<&'a 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: ?Sized + PartialOrd<T>,

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: ?Sized + PartialOrd<T>,

Returns true if item is contained in the range. Read more
1.53.0 · source§

impl<T> SliceIndex<[T]> for (Bound<usize>, Bound<usize>)

§

type Output = [T]

The output type returned by methods.
source§

fn get(self, slice: &[T]) -> Option<&Self::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 Self::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 Self::Output

🔬This is a nightly-only experimental API. (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.
source§

unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output

🔬This is a nightly-only experimental API. (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.
source§

fn index(self, slice: &[T]) -> &Self::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 Self::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.

§

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 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.
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 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.
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.
source§

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

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

source§

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

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

source§

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

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.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.