Accessing tuple elements using dot notation
wirelessringo opened this issue · comments
In the "Data Types" section, a way of accessing the elements of a tuple discussed in this tutorial is through destructuring. Under "Tuple structs", it is suggested that this is the only way of accessing tuple elements:
Their fields must be accessed by destructuring (like a tuple), rather than by name.
However, there is another way of accessing tuple elements: through "dot notation". Tuple elements can be accessed directly by using a period (.
) followed by the index of the value. For example:
fn bar(x: (i32, i32)) {
println!("x was ({}, {})", x.0, x.1); // Note the `x.0` and `x.1`.
}
This can also be applied to tuple structs:
struct IntPoint (i32, i32);
fn foo(x: IntPoint) {
println!("x was ({}, {})", x.0, x.1); // Note the `x.0` and `x.1`.
}
It is quite intriguing to me as to why this is left out, because this syntax is encountered early on in the Rust Programming Language book.