Rust 元组结构的一些用例是什么?
Rust book提到“使用结构几乎总是比使用元组结构更好。”除了新类型模式之外,使用未命名字段还有其他优势吗?在我看来,新类型模式是拥有元组结构的唯一有用案例。它们彼此非常相似 鉴于以下情况 我们可以如下构造结构和元组结构的实例Rust 元组结构的一些用例是什么?,rust,Rust,Rust book提到“使用结构几乎总是比使用元组结构更好。”除了新类型模式之外,使用未命名字段还有其他优势吗?在我看来,新类型模式是拥有元组结构的唯一有用案例。它们彼此非常相似 鉴于以下情况 我们可以如下构造结构和元组结构的实例 let ts = TupleStruct(1, 2); let ns = NormalStruct { a: 1, b: 2 }; // shortcut to initialize the fields of a struct using the values o
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
作业工作如下
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
元组结构的字段具有隐式名称(0,1,…)。因此,访问字段的操作如下所示
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
至少出于文档目的,将显式名称指定给结构的字段几乎总是更清晰的。这就是为什么在Rust社区中,我看到许多人主张始终使用普通结构
但是,也可能存在结构的字段本质上是“匿名”的情况,一个值得注意的情况是“newtype”(具有一个字段的元组结构),其中您只包装了一个内部类型
在这种情况下,命名内部字段不会提供任何附加信息
struct Inches {
inner: i32,
}
vs
有更多关于新类型的信息。它们彼此非常相似 鉴于以下情况 我们可以如下构造结构和元组结构的实例
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
作业工作如下
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
元组结构的字段具有隐式名称(0,1,…)。因此,访问字段的操作如下所示
let ts = TupleStruct(1, 2);
let ns = NormalStruct { a: 1, b: 2 };
// shortcut to initialize the fields of a struct using the values of the
// fields of another struct
let ns2 = NormalStruct { a: 5, ..ns };
let ts2 = TupleStruct { 0: 1, ..ts }; // for TupleStruct it needs curly brackets
// and implicit field names
let TupleStruct(x, y) = ts;
println!("x: {}, y: {}", x, y);
let NormalStruct { a, b } = ns;
println!("a: {}, b: {}", a, b);
println!("Accessing ns by name - {}{}", ns.a, ns.b);
println!("accessing ts by name - {}{}", ts.0, ts.1);
至少出于文档目的,将显式名称指定给结构的字段几乎总是更清晰的。这就是为什么在Rust社区中,我看到许多人主张始终使用普通结构
但是,也可能存在结构的字段本质上是“匿名”的情况,一个值得注意的情况是“newtype”(具有一个字段的元组结构),其中您只包装了一个内部类型
在这种情况下,命名内部字段不会提供任何附加信息
struct Inches {
inner: i32,
}
vs
有更多关于新类型的信息。另一个用例是小算术的数学向量。就像在2d或3d矢量处理库中一样。虽然你也可以用一个newtype来表示数组,但另一个用例是小算术的数学向量。就像在2d或3d矢量处理库中一样。尽管您也可以对there.Thx数组使用newtype。
type Args=(i32,i32)
与struct Args(i32,i32)
相比,前者的偏好是什么?@HelinWang这是两件不同的事情。使用type
可以定义别名(为同一事物创建不同的名称),并且可以在任何地方使用别名。请看,除了存储字段名之外,内存使用和分配是否有显著差异?或者它们只是实现同一事物的两种不同方式?Thx。type Args=(i32,i32)
与struct Args(i32,i32)
相比,前者的偏好是什么?@HelinWang这是两件不同的事情。使用type
可以定义别名(为同一事物创建不同的名称),并且可以在任何地方使用别名。请看,除了存储字段名之外,内存使用和分配是否有显著差异?或者它们只是实现同一事物的两种不同方式?