Skip to content

Instantly share code, notes, and snippets.

@m10e
Created May 29, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save m10e/15e93b31c6c47a5624d9 to your computer and use it in GitHub Desktop.
Save m10e/15e93b31c6c47a5624d9 to your computer and use it in GitHub Desktop.
Rust mod/path question
/*
Everything is in a single crate. The directory structure is as follows:
.
├── b
│   └── mod.rs
├── c
│   └── mod.rs
├── d
│   └── mod.rs
└── main.rs
*/
// -------------------- main.rs
mod b;
mod c;
fn main() {
println!("{}", b::foo());
println!("{}", c::bar());
}
// -------------------- b/mod.rs
#[path="../d/mod.rs"]
mod d;
pub fn foo() -> int {
1 + d::baz()
}
// -------------------- c/mod.rs
#[path="../d/mod.rs"]
mod d;
pub fn bar() -> int {
2 + d::baz()
}
// -------------------- d/mod.rs
pub fn baz() -> int {
10
}
/* My questions are these:
1) In a single crate, is there a way for modules b and c to
access baz() in d without using the #[path] directive
(which, I gather, is non-idiomatic)?
2) If the answer to 1) is no, then is the only way to do this
by putting b and c in one crate and d in another crate? Is that
the idiomatic solution?
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment