当我尝试在另一个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中支持嵌套结构?
答案 0 :(得分:26)
不,他们不受支持。您应该使用单独的struct声明和常规字段:
struct Foo {}
struct Test {
foo: Foo,
}