I\'m trying to figure out how to compile multi-file crates in Rust, but I keep getting a compile error.
I have the file I want to import into the crate thing.rs:
You just need to put the use
at the top of the file:
use thing::asdf::*;
mod thing;
fn main() {}
This looks very strange, but
use
or extern mod
is an "item", including mod
s), and use
is always relative to the top of the crate, and the whole crate is loaded before name resolution happens, so use thing::asdf::*;
makes rustc look for thing
as a submodule of the crate (which it finds), and then asdf
as a submodule of that, etc.To illustrate this last point better (and demonstrate the two special names in use
, super
and self
, which import directly from the parent and current module respectively):
// crate.rs
pub mod foo {
// use bar::baz; // (an error, there is no bar at the top level)
use foo::bar::baz; // (fine)
// use self::bar::baz; // (also fine)
pub mod bar {
use super::qux; // equivalent to
// use foo::qux;
pub mod baz {}
}
pub mod qux {}
}
fn main() {}
(Also, tangentially, the .rc
file extension no longer has any special meaning to any Rust tools (including in 0.6), and is deprecated, e.g. all the .rc
files in the compiler source tree were recently renamed to .rs
.)