Primitive Type tuple [−]
Operations on tuples
To access the N-th element of a tuple one can use N
`N` itself
as a field of the tuple.
Indexing starts from zero, so 0
`0returns first value,
` returns first value, 1
`1returns second value, and so on. In general, a tuple with _S_ elements provides aforementioned fields from
`
returns second value, and so on. In general, a tuple with S
elements provides aforementioned fields from 0
`0to
` to S-1
`S-1`.
If every type inside a tuple implements one of the following traits, then a tuple itself also implements it.
Clone
`Clone`PartialEq
`PartialEq`Eq
`Eq`PartialOrd
`PartialOrd`Ord
`Ord`Default
`Default`
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));