As per this
Tuple structs, which are, basically, named tuples.
// A tuple struct
struct Pair(i32, f32);
Later in the code
// Instantiate a tuple struct
let pair = Pair(1, 0.1);
// Access the fields of a tuple struct
println!("pair contains {:?} and {:?}", pair.0, pair.1);
If this is a "named tuple" why am I accessing it using .0
and .1
? How is that different from a "normal tuple"?
let pair = (1, 0.1);
println!("pair contains {:?} and {:?}", pair.0, pair.1);
In Python a named tuple has a name and also allows access via index
from collections import namedtuple
Pair = namedtuple('Pair', ['x', 'y'])
pair = Pair(1, 0.1)
print(pair[0], pair[1]) # 1 0.1
print(pair.x, pair.y) # 1 0.1
So the question, what is the "name" in the "named tuple" in the above rust example? To me the "The classic C structs" (in the same link) is what sounds like a "named tuple" as I can access it using .x
and .y
if I initialised the struct (Pair
) as such. I fail to understand this from the link.
Pair
here), but its fields are anonymous. That's unlike a normal tuple, where nothing is named, and a normal ("classic") struct, where both the struct and its fields have names. And well, python does this differently. Such is life with nomenclatures. – Foiltype Pair = (i32, f32);
.) – Foil