rust - How to define a multi level crate? -
i'm trying define crate going along rust application explained in documentation. let's take following directory structure:
src/ ├─ lib.rs ├─ main.rs └─ myapp/ ├─ a/ │ ├─ a1.rs │ └─ mod.rs └─ mod.rs now defined following in rust files:
lib.rs
pub mod myapp; main.rs
extern crate myapp; use myapp::a; fn main() { unimplemented!(); } myapp/mod.rs
pub mod a; myapp/a/mod.rs
pub use self::a1::*; mod a1; myapp/a/a1.rs
pub fn myfunc() { unimplemented!(); } if try compile above directory tree, got error:
$ cargo build compiling myapp v0.1.0 src/main.rs:2:5: 2:13 error: unresolved import `myapp::a`. there no `a` in `myapp` [e0432] src/main.rs:2 use myapp::a; ^~~~~~~~ src/main.rs:2:5: 2:13 help: run `rustc --explain e0432` see detailed explanation error: aborting due previous error not compile `myapp`. where wrong here? think have same directory structure 1 shown in documentation.
you've added level of nesting. main works if change be
extern crate myapp; use myapp::myapp::a; fn main() { unimplemented!(); } crates form own level in path, first myapp refers crate, second myapp refers module you've created.
on side note, it's rare see crate level of nesting , bifurcation. rust crates tend have larger chunks of code per file, , not one-struct-per-file common in other languages. in experience, of course, , isn't hard-and-fast rule.
Comments
Post a Comment