Rust中是否支持嵌套结构?

时间:2014-05-13 10:51:23

标签: struct rust

当我尝试在另一个struct中声明一个struct时:

struct Test {
    struct Foo {}
}

编译器抱怨:

error: expected identifier, found keyword `struct`
 --> src/lib.rs:2:5
  |
2 |     struct Foo {}
  |     ^^^^^^ expected identifier, found keyword
help: you can escape reserved keywords to use them as identifiers
  |
2 |     r#struct Foo {}
  |     ^^^^^^^^

error: expected `:`, found `Foo`
 --> src/lib.rs:2:12
  |
2 |     struct Foo {}
  |            ^^^ expected `:`

我找不到任何方向的文件;是否在Rust中支持嵌套结构?

1 个答案:

答案 0 :(得分:26)

不,他们不受支持。您应该使用单独的struct声明和常规字段:

struct Foo {}

struct Test {
    foo: Foo,
}