Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Rust source files for cargo

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?

like image 694
fadedbee Avatar asked Oct 22 '14 12:10

fadedbee


People also ask

How do you add Cargo in Rust?

The easiest way to get Cargo is to install the current stable release of Rust by using rustup. Installing Rust using rustup will also install cargo . It will download a script, and start the installation.

Where is the Cargo config file?

Windows: %USERPROFILE%\. cargo\config. toml.

What is Cargo toml in Rust?

Cargo. toml is the manifest file for Rust's package manager, cargo . This file contains metadata such as name, version, and dependencies for packages, which are call "crates" in Rust.

What is Cargo NEW -- bin?

cargo new accepts two flags: --lib, for creating libraries, and --bin, for creating binaries, or executables.


2 Answers

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.

like image 105
Chris Morgan Avatar answered Nov 10 '22 16:11

Chris Morgan


It's hard to say what you're getting tripped up on from the info you shared. Here are three seemingly trivial things that I still had to refer to the documentaton to figure out:

First of all,

    mod foo;

looks like a declaration, but it without arguments it is actually something like an include. So you use the same keyword both for declaring and including modules, i.e. there is no using:: keyword.

Second, modules themselves can be public or private. If you didn't add a pub keyword both on the function in question AND on the containing module, that may be tripping you up.

    pub mod foo {pub fn bar();}

Third, there seems to be an implicit module added at the top of every file. This is confusing; the reference manual talks about a strict separation between file paths and names, and the module paths in your code, but that abstraction seems to be leaky here.

Note, Rust is still pre-1.0 (0.12) at the time of writing, at the module system and file paths are relatively high level, so don't be surprised if what I said may already wrong by the time you read this.

like image 38
Andrew Wagner Avatar answered Nov 10 '22 16:11

Andrew Wagner