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
:
â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 implementationsIn 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.
§ExamplesBasic 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) {
(4, 5)
}
let point = calculate_point();
assert_eq!(point.0, 4);
assert_eq!(point.1, 5);
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();
1.0.0 · Source§
This trait is implemented for tuples up to twelve items long.
1.0.0 · Source§This trait is implemented for tuples up to twelve items long.
1.2.0 · Source§ Source§Extends a collection with the contents of an iterator.
Read more Source§ ð¬This is a nightly-only experimental API. (extend_one
#72631)
Extends a collection with exactly one element.
Source§ ð¬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§ Source§Extends a collection with the contents of an iterator.
Read more Source§ ð¬This is a nightly-only experimental API. (extend_one
#72631)
Extends a collection with exactly one element.
Source§ ð¬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§This trait is implemented for tuples up to twelve items long. The impl
s for 1- and 3- through 12-ary tuples were stabilized after 2-tuples, in 1.85.0.
Allows to extend
a tuple of collections that also implement Extend
.
See also: Iterator::unzip
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]);
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§ ð¬This is a nightly-only experimental API. (extend_one
#72631)
Extends a collection with exactly one element.
Source§ ð¬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§ Source§Extends a collection with the contents of an iterator.
Read more Source§ ð¬This is a nightly-only experimental API. (extend_one
#72631)
Extends a collection with exactly one element.
Source§ ð¬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§Inserts all new key-values from the iterator and replaces values with existing keys with new values returned from the iterator.
Source§Extends a collection with the contents of an iterator.
Read more Source§ ð¬This is a nightly-only experimental API. (extend_one
#72631)
Extends a collection with exactly one element.
Source§ ð¬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§This trait is implemented for tuples up to twelve items long.
Source§Converts to this type from the input type.
1.17.0 (const: unstable) · Source§ 1.71.0 · Source§This trait is implemented for tuples up to twelve items long.
Source§Converts to this type from the input type.
1.79.0 · Source§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]);
This trait is implemented for tuples up to twelve items long. The impl
s for 1- and 3- through 12-ary tuples were stabilized after 2-tuples, in 1.85.0.
Constructs a BTreeMap<K, V>
from an iterator of key-value pairs.
If the iterator produces any pairs with equal keys, all but one of the corresponding values will be dropped.
1.0.0 · Source§ Source§Constructs a HashMap<K, V>
from an iterator of key-value pairs.
If the iterator produces any pairs with equal keys, all but one of the corresponding values will be dropped.
1.0.0 · Source§This trait is implemented for tuples up to twelve items long.
Source§ Source§ ð¬This is a nightly-only experimental API. (range_into_bounds
#136903)
Convert this range into the start and end bounds. Returns
(start_bound, end_bound)
.
Read more Source§ ð¬This is a nightly-only experimental API. (range_into_bounds
#136903)
Compute the intersection of
self
and
other
.
Read more 1.0.0 · Source§This trait is implemented for tuples up to twelve items long.
1.0.0 · Source§This trait is implemented for tuples up to twelve items long.
Source§Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This trait is implemented for tuples up to twelve items long.
Source§This method returns an ordering between
self
and
other
values if one exists.
Read more Source§Tests less than (for
self
and
other
) and is used by the
<
operator.
Read more Source§Tests less than or equal to (for
self
and
other
) and is used by the
<=
operator.
Read more Source§Tests greater than or equal to (for
self
and
other
) and is used by the
>=
operator.
Read more Source§Tests greater than (for
self
and
other
) and is used by the
>
operator.
Read more 1.28.0 · Source§ Source§ Source§ 1.35.0 · Source§Returns
true
if
item
is contained in the range.
Read more Source§ ð¬This is a nightly-only experimental API. (range_bounds_is_empty
#137300)
Returns
true
if the range contains no items. One-sided ranges (
RangeFrom
, etc) always return
false
.
Read more 1.28.0 · Source§ Source§ Source§ 1.35.0 · Source§Returns
true
if
item
is contained in the range.
Read more Source§ ð¬This is a nightly-only experimental API. (range_bounds_is_empty
#137300)
Returns
true
if the range contains no items. One-sided ranges (
RangeFrom
, etc) always return
false
.
Read more 1.53.0 · Source§ Source§The output type returned by methods.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a shared reference to the output at this location, if in bounds.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a mutable reference to the output at this location, if in bounds.
Source§ð¬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§ð¬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§ð¬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§ð¬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§ Source§The output type returned by methods.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a shared reference to the output at this location, if in bounds.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a mutable reference to the output at this location, if in bounds.
Source§ð¬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§ð¬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§ð¬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§ð¬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§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).
§PanicsPanics 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
.
The output type returned by methods.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a shared reference to the output at this location, if in bounds.
Source§ð¬This is a nightly-only experimental API. (slice_index_methods
)
Returns a mutable reference to the output at this location, if in bounds.
Source§ð¬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§ð¬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§ð¬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§ð¬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§ Source§Returned iterator over socket addresses which this type may correspond to.
Source§ 1.0.0 · Source§ Source§Returned iterator over socket addresses which this type may correspond to.
Source§ 1.0.0 · Source§ Source§Returned iterator over socket addresses which this type may correspond to.
Source§ 1.0.0 · Source§ Source§Returned iterator over socket addresses which this type may correspond to.
Source§ 1.46.0 · Source§ Source§Returned iterator over socket addresses which this type may correspond to.
Source§ Source§This trait is implemented for tuples up to twelve items long.
1.0.0 · Source§This trait is implemented for tuples up to twelve items long.
Source§This trait is implemented for tuples up to twelve items long.
Source§This trait is implemented for tuples up to twelve items long.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4