rust - How can structs in separate files reference each other? -


using structs reference each other common in single file, when separate structs 2 files, error.

mod_1.rs

mod mod_2; use mod_2::haha;  pub struct hehe {     obj: haha, }  fn main() {     hehe(haha); } 

mod_2.rs

mod mod_1; use mod_1::hehe;  pub struct haha {     obj: hehe, }  fn main() {     haha(hehe); } 

this produce error. when compiling mod_1.rs:

error: cannot declare new module @ location  --> mod_2.rs:1:5   | 1 | mod mod_1;   |     ^^^^^   | note: maybe move module `mod_2` own directory via `mod_2/mod.rs`  --> mod_2.rs:1:5   | 1 | mod mod_1;   |     ^^^^^ note: ... or maybe `use` module `mod_1` instead of possibly redeclaring  --> mod_2.rs:1:5   | 1 | mod mod_1;   |     ^^^^^ 

when compiling mod_2.rs:

error: cannot declare new module @ location  --> mod_1.rs:1:5   | 1 | mod mod_2;   |     ^^^^^   | note: maybe move module `mod_1` own directory via `mod_1/mod.rs`  --> mod_1.rs:1:5   | 1 | mod mod_2;   |     ^^^^^ note: ... or maybe `use` module `mod_2` instead of possibly redeclaring  --> mod_1.rs:1:5   | 1 | mod mod_2;   |     ^^^^^ 

in mod_1.rs use mod_2.rs , in mod_2.rs, use mod_1.rs's thing, i'd find way rid of cycle reference module problem.

trying rust load files similar different problem.

this common misunderstanding of rust's module system. basically, there 2 steps:

  1. you have build module-tree. implies there not cycles in module-tree , there clear parent-child relationship between nodes. step tell rust files load , has nothing usage of symbols in different modules.

  2. you can reference each symbol in module tree path (where each module , final symbol name separated ::, example std::io::read). in order avoid writing full path every time, can use use declarations refer specific symbols simple name.

you can read bit more on in rust book chapter.

and again, avoid confusion: in order use symbol module, don't have write mod my_module; in module! each non-root module of project there one line in entire project saying mod said_module; (the root module not have such line @ all). once!


about example: first compiled mod_1.rs via rustc mod_1.rs. means mod_1 root module in case. explained above, root module doesn't need declared via mod @ all, other modules need declared exactly once. means mod mod_2; in mod_1.rs correct, mod mod_1; in mod_2.rs incorrect. compiler suggest right thing do:

note: ... or maybe `use` module `mod_2` instead of possibly redeclaring 

you useing it, can remove line mod mod_1; , solve error.

however, think still thinking incorrectly module system. mentioned above first need design more or less fixed module tree implies have one fixed root module. whatever pass rustc root module , doesn't make sense use different modules root module. in project there should 1 fixed root module. mod_1 explained above. it's more idiomatic call lib libraries , main executables.

so again: first, draw module tree on piece of paper. consider fixed moment , then can create files , mod declarations appropriately.


last thing: when fixing module system, example won't work, because haha , hehe types infinite size. fields of structs directly put memory layout of struct (without boxing them in java!). can't have cycles in struct definitions, except if manually add layer of indirection, boxing fields. please read this excellent explanation on issue.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -