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

Trait Implementations

This trait is implemented on arbitrary-length tuples.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Formats the value using the given formatter. Read more

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

Returns the “default value” for a type. Read more

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

🔬 This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements. Read more

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

🔬 This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements. Read more

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

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements. Read more

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

🔬 This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements. Read more

Inserts all new key-values from the iterator and replaces values with existing keys with new values returned from the iterator.

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

🔬 This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements. Read more

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.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

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

Feeds this value into the given Hasher. Read more

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

This method returns an Ordering between self and other. Read more

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

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

This method tests for !=.

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

This method returns an ordering between self and other values if one exists. Read more

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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

Start index bound. Read more

End index bound. Read more

Returns true if item is contained in the range. Read more

Start index bound. Read more

End index bound. Read more

Returns true if item is contained in the range. Read more

The output type returned by methods.

🔬 This is a nightly-only experimental API. (slice_index_methods)

Returns a shared reference to the output at this location, if in bounds. Read more

🔬 This is a nightly-only experimental API. (slice_index_methods)

Returns a mutable reference to the output at this location, if in bounds. Read more

🔬 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. Read more

🔬 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. Read more

🔬 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. Read more

🔬 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. Read more

Returned iterator over socket addresses which this type may correspond to. Read more

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

Returned iterator over socket addresses which this type may correspond to. Read more

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

Returned iterator over socket addresses which this type may correspond to. Read more

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

Returned iterator over socket addresses which this type may correspond to. Read more

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

Returned iterator over socket addresses which this type may correspond to. Read more

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

This trait is implemented on arbitrary-length tuples.

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.