Pregunta

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
¿Fue útil?

Solución

You're being caught by incorrect usage of mod.

mod defines a module.

use imports an already defined module.

What you should be using is something like your lib.rs or mod.rs or whatever containing mod a; and mod b;, and then, in a.rs, use b::B;, and in b.rs, use a::A.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top