繁体   English   中英

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

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

当我写作

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

它汇编。

我如何在模块和文件之间拆分?

当我尝试

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

我明白了

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

这是我的环境

$ 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

你被不正确的mod使用所mod

mod 定义了一个模块。

use导入已定义的模块。

你应该使用的是像你的lib.rsmod.rs或任何包含mod a; mod b; ,然后,在a.rsuse b::B; ,在b.rsuse a::A

暂无
暂无

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

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