Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

结构体表达式

Syntax
StructExpression
    PathInExpression { ( StructExprFields | StructBase )? }

StructExprFields
    StructExprField ( , StructExprField )* ( , StructBase | ,? )

StructExprField
    OuterAttribute*
    (
        IDENTIFIER
      | ( IDENTIFIER | TUPLE_INDEX ) : Expression
    )

StructBase.. Expression

结构体表达式创建结构体、枚举或联合体值。它由指向结构体枚举变体联合体项的路径后跟该项的字段值组成。

以下是结构体表达式的示例:

#![allow(unused)]
fn main() {
struct Point { x: f64, y: f64 }
struct NothingInMe { }
mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
enum Enum { Variant {} }
Point {x: 10.0, y: 20.0};
NothingInMe {};
let u = game::User {name: "Joe", age: 35, score: 100_000};
Enum::Variant {};
}

Note

元组结构体和元组枚举变体通常使用引用值命名空间中构造函数调用表达式实例化。这与使用花括号引用类型命名空间中构造函数的结构体表达式不同。

#![allow(unused)]
fn main() {
struct Position(i32, i32, i32);
Position(0, 0, 0);  // Typical way of creating a tuple struct.
let c = Position;  // `c` is a function that takes 3 arguments.
let pos = c(8, 6, 7);  // Creates a `Position` value.

enum Version { Triple(i32, i32, i32) };
Version::Triple(0, 0, 0);
let f = Version::Triple;
let ver = f(8, 6, 7);
}

调用路径的最后一段不能引用类型别名:

#![allow(unused)]
fn main() {
trait Tr { type T; }
impl<T> Tr for T { type T = T; }

struct Tuple();
enum Enum { Tuple() }

// <Unit as Tr>::T(); // causes an error -- `::T` is a type, not a value
<Enum as Tr>::T::Tuple(); // OK
}

单元结构体和单元枚举变体通常使用引用值命名空间中常量路径表达式实例化。

#![allow(unused)]
fn main() {
struct Gamma;
// Gamma unit value, referring to the const in the value namespace.
let a = Gamma;
// Exact same value as `a`, but constructed using a struct expression
// referring to the type namespace.
let b = Gamma {};

enum ColorSpace { Oklch }
let c = ColorSpace::Oklch;
let d = ColorSpace::Oklch {};
}

字段结构体表达式

用花括号括起字段的结构体表达式允许您按任何顺序为每个单独的字段指定值。字段名用冒号与其值分隔。

联合体类型的值只能使用此语法创建,并且必须指定恰好一个字段。

函数式更新语法

构造结构体类型值的结构体表达式可以用语法 .. 后跟表达式来终止,以表示函数式更新。

.. 后面的表达式(基)必须与正在形成的新结构体类型具有相同的结构体类型。

整个表达式对指定的字段使用给定的值,并从基表达式移动或复制剩余的字段。

与所有结构体表达式一样,结构体的所有字段必须是可见的,即使是未显式命名的字段。

#![allow(unused)]
fn main() {
struct Point3d { x: i32, y: i32, z: i32 }
let mut base = Point3d {x: 1, y: 2, z: 3};
let y_ref = &mut base.y;
Point3d {y: 0, z: 10, .. base}; // OK, only base.x is accessed
drop(y_ref);
}

结构体表达式不能直接在循环if 表达式的头部,或在 if letmatch 表达式的审查者中使用。但是,如果它们在另一个表达式内部,例如在括号内,则可以在这些情况下使用结构体表达式。

字段名可以是十进制整数值,以指定构造元组结构体的索引。这可以与基结构体一起使用,以填充未指定的剩余索引:

#![allow(unused)]
fn main() {
struct Color(u8, u8, u8);
let c1 = Color(0, 0, 0);  // Typical way of creating a tuple struct.
let c2 = Color{0: 255, 1: 127, 2: 0};  // Specifying fields by index.
let c3 = Color{1: 0, ..c2};  // Fill out all other fields using a base struct.
}

结构体字段初始化简写

当使用命名(但不是编号)字段初始化数据结构(结构体、枚举、联合体)时,允许编写 fieldname 作为 fieldname: fieldname 的简写。这允许使用更紧凑的语法,减少重复。例如:

#![allow(unused)]
fn main() {
struct Point3d { x: i32, y: i32, z: i32 }
let x = 0;
let y_value = 0;
let z = 0;
Point3d { x: x, y: y_value, z: z };
Point3d { x, y: y_value, z };
}