简体   繁体   English

如何在Rust中的模块和文件中定义循环结构?

[英]How do I define cyclical structs across modules and files in Rust?

When I write 当我写作

struct A { b : Option<B> }
struct B { a : Option<A> }

it compiles. 它汇编。

How would I split this up across modules and files? 我如何在模块和文件之间拆分?

When I try 当我尝试

// a.rs
mod b;
struct A { b : Option<B> }
// b.rs
mod a;
struct B { a : Option<A> }

I get 我明白了

$ rustc a.rs 
a.rs:1:5: 1:6 error: circular modules: b.rs -> a.rs -> b.rs
a.rs:1 mod b;
           ^

Here is my environment 这是我的环境

$ rustc --version
rustc 0.11-pre-nightly (168b2d1 2014-04-14 14:36:54 -0700)
host: x86_64-unknown-linux-gnu
$ uname -a
Linux 3.14.1-1-ARCH #1 SMP PREEMPT Mon Apr 14 20:40:47 CEST 2014 x86_64 GNU/Linux

You're being caught by incorrect usage of mod . 你被不正确的mod使用所mod

mod defines a module. mod 定义了一个模块。

use imports an already defined module. use导入已定义的模块。

What you should be using is something like your lib.rs or mod.rs or whatever containing mod a; 你应该使用的是像你的lib.rsmod.rs或任何包含mod a; and mod b; mod b; , and then, in a.rs , use b::B; ,然后,在a.rsuse b::B; , and in b.rs , use a::A . ,在b.rsuse a::A

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM