Primitive Type tuple [−]
A finite heterogeneous sequence, (T, U, ..)
.
To access the N-th element of a tuple one can use N
itself
as a field of the tuple.
Indexing starts from zero, so 0
returns first value, 1
returns second value, and so on. In general, a tuple with S
elements provides aforementioned fields from 0
to S-1
.
If every type inside a tuple implements one of the following traits, then a tuple itself also implements it.
Clone
PartialEq
Eq
PartialOrd
Ord
Debug
Default
Hash
Examples
Accessing elements of a tuple at specified indices:
fn main() { let x = ("colorless", "green", "ideas", "sleep", "furiously"); assert_eq!(x.3, "sleep"); let v = (3, 3); let u = (1, -5); assert_eq!(v.0 * u.0 + v.1 * u.1, -12); }let x = ("colorless", "green", "ideas", "sleep", "furiously"); assert_eq!(x.3, "sleep"); let v = (3, 3); let u = (1, -5); assert_eq!(v.0 * u.0 + v.1 * u.1, -12);
Using traits implemented for tuples:
fn main() { let a = (1, 2); let b = (3, 4); assert!(a != b); let c = b.clone(); assert!(b == c); let d : (u32, f32) = Default::default(); assert_eq!(d, (0, 0.0f32)); }let a = (1, 2); let b = (3, 4); assert!(a != b); let c = b.clone(); assert!(b == c); let d : (u32, f32) = Default::default(); assert_eq!(d, (0, 0.0f32));