为什么生锈的 "tuple struct" 叫做 "named tuple"

Why is a "tuple struct" in rust called "named tuple"

根据this

Tuple structs, which are, basically, named tuples.

// A tuple struct
struct Pair(i32, f32);

稍后在代码中

// 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);

如果这是一个“命名元组”,为什么我要使用 .0.1 访问它?这与“普通元组”有何不同?

let pair = (1, 0.1);
println!("pair contains {:?} and {:?}", pair.0, pair.1);

在 Python 中,命名元组有一个名称,也允许通过索引进行访问

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

那么问题来了,上面rust例子中的“named tuple”中的“name”是什么?对我来说,“经典 C 结构”(在同一个 link 中)听起来像一个“命名元组”,因为如果我初始化结构,我可以使用 .x.y 访问它(Pair) 就这样。我无法从 link.

中理解这一点

Tuple structs, which are, basically, named tuples.

命名的不是实例或成员,而是整个类型。

How is that different from a "normal tuple"?

接受元组结构的函数将不接受常规元组,并且vice-versa。

struct Named(f32,i32);
fn accepts_tuple(t:(f32,i32)) { todo!(); }
fn accepts_named(t:Named) { todo!(); }

fn main() {
  let t = (1.0f32, 1i32);
  accepts_tuple(t); // OK
  // accepts_named(t); // Does not compile
  let n=Named(1.0f32, 1i32);
  // accepts_tuple(n); // Does not compile
  accepts_named(n); // OK
}