Multiple Rust source files for cargo

fadedbee picture fadedbee · Oct 22, 2014 · Viewed 7.6k times · Source

If I have multiple .rs files in the src directory of a Cargo package, what are the rules for visibility, importing, etc.?

Currently, any extra (i.e. not the file that is explicitly identified as the source for the executable in Cargo.toml) files are ignored.

What do I need to do to fix this?

Answer

Chris Morgan picture Chris Morgan · Oct 22, 2014

There is nothing special about Cargo at all in this way. It’s all the perfectly normal Rust module system. If Cargo will be compiling src/lib.rs, that’s more or less equivalent to having executed rustc --crate-type lib src/lib.rs (there are more command line arguments in practice, but that’s the basics of it).

Other files are then used with mod, use and so forth. Files are not automatically imported or anything like that. This part is not documented very clearly yet; a couple of things that show briefly how to achieve things are http://rustbyexample.com/mod/split.html and http://doc.rust-lang.org/reference.html#modules, but any non-trivial code base will use them and so you can pick just about any code base to look at for examples.