For now, this reference is a best-effort document. We strive for validity and completeness, but are not yet there. In the future, the docs and lang teams will work together to figure out how best to do this. Until then, this is a best-effort attempt. If you find something wrong or missing, file an issue or send in a pull request.

Tuple and tuple indexing expressions

Tuple expressions

Tuples are written by enclosing zero or more comma-separated expressions in parentheses. They are used to create tuple-typed values.


# #![allow(unused_variables)]
#fn main() {
(0.0, 4.5);
("a", 4usize, true);
();
#}

You can disambiguate a single-element tuple from a value in parentheses with a comma:


# #![allow(unused_variables)]
#fn main() {
(0,); // single-element tuple
(0); // zero in parentheses
#}

Tuple indexing expressions

Tuples and struct tuples can be indexed using the number corresponding to the position of the field. The index must be written as a decimal literal with no underscores or suffix. Tuple indexing expressions also differ from field expressions in that they can unambiguously be called as a function. In all other aspects they have the same behavior.


# #![allow(unused_variables)]
#fn main() {
# struct Point(f32, f32);
let pair = (1, 2);
assert_eq!(pair.1, 2);
let unit_x = Point(1.0, 0.0);
assert_eq!(unit_x.0, 1.0);
#}