Primitive Type tuple
1.0.0 ·Expand description
一个有限异构序列,(T, U, ..)
。
让我们依次介绍其中的每一个:
元组是有限的。换句话说,元组具有长度。这是长度为 3
的元组:
("hello", 5, 'c');
Run‘Length’ 有时也称为 ‘arity’; 每个不同长度的元组都是不同的,不同的类型。
元组是异构的。这意味着元组的每个元素可以具有不同的类型。 在上面的元组中,其类型为:
(&'static str, i32, char)
Run元组是一个序列。这意味着可以按位置访问它们; 这称为元组索引,它看起来像这样:
let tuple = ("hello", 5, 'c');
assert_eq!(tuple.0, "hello");
assert_eq!(tuple.1, 5);
assert_eq!(tuple.2, 'c');
Run元组的顺序性质适用于各种 traits 的实现。
例如,在 PartialOrd
和 Ord
中,元素按顺序进行比较,直到找到第一个不相等的集合。
有关元组的更多信息,请参见 这本书。
trait 实现
在本文档中,简写 (T₁, T₂,…, Tₙ)
用于表示不同长度的元组。
当使用它时,在 T
上表达的任何 trait bound 都独立地应用于元组的每个元素。
请注意,这是一种方便的符号,以避免重复文档,不是有效的 Rust 语法。
由于 Rust 的类型系统的临时限制,以下 traits 仅在 arity 12 或更少的元组上实现。 在未来,这可能会改变:
以下 traits 用于任意长度的元组。 这些 traits 具有由编译器自动生成的实现,因此不受缺少语言特性的限制。
Examples
基本用法:
let tuple = ("hello", 5, 'c');
assert_eq!(tuple.0, "hello");
Run当您要返回多个值时,通常将元组用作返回类型:
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);
Run可以从适当长度的数组创建同构元组:
let array: [u32; 3] = [1, 2, 3];
let tuple: (u32, u32, u32) = array.into();
RunTrait Implementations§
source§impl<T: Clone> Clone for (T₁, T₂, …, Tₙ)
impl<T: Clone> Clone for (T₁, T₂, …, Tₙ)
这个 trait 在任意长度的元组上实现。
source§impl<T> Debug for (T₁, T₂, …, Tₙ)where
T: ?Sized + Debug,
impl<T> Debug for (T₁, T₂, …, Tₙ)where T: ?Sized + Debug,
This trait is implemented for tuples up to twelve items long.
source§impl<T: Default> Default for (T₁, T₂, …, Tₙ)
impl<T: Default> Default for (T₁, T₂, …, Tₙ)
This trait is implemented for tuples up to twelve items long.
1.56.0 · source§impl<A, B, ExtendA, ExtendB> Extend<(A, B)> for (ExtendA, ExtendB)where
ExtendA: Extend<A>,
ExtendB: Extend<B>,
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)
fn extend<T: IntoIterator<Item = (A, B)>>(&mut self, into_iter: T)
允许 extend
一个集合的元组也实现 Extend
。
另请参见: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]);
// 还允许任意嵌套的元组作为元素
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]);
Runsource§fn extend_one(&mut self, item: (A, B))
fn extend_one(&mut self, item: (A, B))
extend_one
#72631)1.71.0 · source§impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 4]> for (T, T, T, T)
impl<T> From<[T; 4]> for (T, T, T, T)
1.71.0 · source§impl<T> From<[T; 5]> for (T, T, T, T, T)
impl<T> From<[T; 5]> for (T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 6]> for (T, T, T, T, T, T)
impl<T> From<[T; 6]> for (T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)
impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)
impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)
1.71.0 · source§impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)
1.17.0 · source§impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
source§fn from(pieces: (I, u16)) -> SocketAddr
fn from(pieces: (I, u16)) -> SocketAddr
将元组结构体 (Into <IpAddr
>,u16
) 转换为 SocketAddr
。
此转换为 IpAddr::V4
创建 SocketAddr::V4
,并为 IpAddr::V6
创建 SocketAddr::V6
。
u16
被视为新创建的 SocketAddr
的端口。
1.71.0 · source§impl<T> From<(T, T, T, T)> for [T; 4]
impl<T> From<(T, T, T, T)> for [T; 4]
source§fn from(tuple: (T, T, T, T)) -> Self
fn from(tuple: (T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T)> for [T; 5]
impl<T> From<(T, T, T, T, T)> for [T; 5]
source§fn from(tuple: (T, T, T, T, T)) -> Self
fn from(tuple: (T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T)> for [T; 6]
impl<T> From<(T, T, T, T, T, T)> for [T; 6]
source§fn from(tuple: (T, T, T, T, T, T)) -> Self
fn from(tuple: (T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T)> for [T; 7]
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
fn from(tuple: (T, T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T, T)> for [T; 8]
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
fn from(tuple: (T, T, T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T, T, T)> for [T; 9]
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
fn from(tuple: (T, T, T, T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T, T, T, T)> for [T; 10]
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
fn from(tuple: (T, T, T, T, T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T, T, T, T, T)> for [T; 11]
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
fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T)) -> Self
1.71.0 · source§impl<T> From<(T, T, T, T, T, T, T, T, T, T, T, T)> for [T; 12]
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
fn from(tuple: (T, T, T, T, T, T, T, T, T, T, T, T)) -> Self
source§impl<T> Hash for (T₁, T₂, …, Tₙ)where
T: ?Sized + Hash,
impl<T> Hash for (T₁, T₂, …, Tₙ)where T: ?Sized + Hash,
This trait is implemented for tuples up to twelve items long.
source§impl<T> Ord for (T₁, T₂, …, Tₙ)where
T: ?Sized + Ord,
impl<T> Ord for (T₁, T₂, …, Tₙ)where T: ?Sized + Ord,
This trait is implemented for tuples up to twelve items long.
source§impl<T> PartialEq<(T,)> for (T₁, T₂, …, Tₙ)where
T: ?Sized + PartialEq,
impl<T> PartialEq<(T,)> for (T₁, T₂, …, Tₙ)where T: ?Sized + PartialEq,
This trait is implemented for tuples up to twelve items long.
source§impl<T> PartialOrd<(T,)> for (T₁, T₂, …, Tₙ)where
T: ?Sized + PartialOrd,
impl<T> PartialOrd<(T,)> for (T₁, T₂, …, Tₙ)where T: ?Sized + PartialOrd,
This trait is implemented for tuples up to twelve items long.
1.28.0 · 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§fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get(self, slice: &[T]) -> Option<&Self::Output>
slice_index_methods
)source§fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
slice_index_methods
)source§unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
slice_index_methods
)slice
指针调用此方法也是 [undefined 行为]。source§unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
slice_index_methods
)slice
指针调用此方法也是 [undefined 行为]。impl<T: Copy> Copy for (T₁, T₂, …, Tₙ)
这个 trait 在任意长度的元组上实现。
impl<T> Eq for (T₁, T₂, …, Tₙ)where T: ?Sized + Eq,
This trait is implemented for tuples up to twelve items long.