Sub topic 2
Reading tuple fields with .0, .1, and so on
This chapter assumes you know basic Rust syntax, scalar types such as i32 and f64, and how to run a program with cargo. It covers how to read a single field out of a tuple with a numeric index.
Dot-index syntax
A tuple field is read by writing a dot and then the field's position, counted from zero. The position is a literal number written in the source, not a variable.
fn main() {
let point: (i32, f64, &str) = (10, 2.5, "origin");
println!("{}", point.0);
println!("{}", point.1);
println!("{}", point.2);
}This prints 10, 2.5, and origin. Each expression has the type of its field: point.0 is an i32, point.1 is an f64, and point.2 is a &str.
Why the index must be a literal
Fields of a tuple can have different types. The compiler therefore has to know which field you mean in order to know the type of the result. A runtime value such as point[i] cannot give it that. This is why tuples use .0 and not [0], and why the following does not compile:
fn main() {
let point = (10, 2.5, "origin");
let i = 1;
println!("{}", point.i);
}The compiler reports that point has no field named i. If you need to pick elements by a runtime index, use an array, where every element has the same type.
Out-of-range indexes are compile errors
The tuple's length is part of its type, so the compiler checks every .N against it. Reading point.3 on a three-field tuple fails to compile with a "no field 3 on type" error. There is no runtime panic and no Option to handle, because the mistake is caught before the program runs.
Assigning to a field
If the tuple binding is mut, you can assign to a field. The new value must have the field's type.
fn main() {
let mut point = (10, 2.5, "origin");
point.0 += 5;
point.2 = "moved";
println!("{:?}", point);
}This prints (15, 2.5, "moved"). Without mut, the assignment is rejected.
Nested tuples
When a tuple contains another tuple, chain the accesses from left to right.
fn main() {
let segment = ((0, 0), (3, 4));
println!("{}", (segment.1).0);
println!("{}", segment.1.0);
}Both lines print 3. Writing segment.1.0 works in current Rust. Older code sometimes uses parentheses, as in (segment.1).0, because early lexers read 1.0 as a floating-point number.
Common mistakes
- Using square brackets, as in
point[0]. Tuples do not support[]indexing. - Using a variable after the dot. The index must be a literal number.
- Counting from one. The first field is
.0. - Reading past the end.
.NwithNequal to or greater than the tuple's length is a compile error.
Exercise
Given let t = (1, (2.5, 'x'), "hi");, which expression gives the char 'x', and why can't you write t[1][1]?
Show answer
t.1.1 gives 'x'. Field 1 of t is the inner tuple (2.5, 'x'), and field 1 of that is 'x'. Tuples are not indexed with [] because their fields can have different types, so the compiler needs a literal position to know the result type.
Exercise
What happens if you compile let t = (1, 2); println!("{}", t.2);?
Show answer
It fails to compile. The tuple has only fields .0 and .1, and the compiler checks the index against the tuple's type, so there is no runtime panic.