How do I include a file with the full path my_project/src/include_me.rs
in main.rs
?
I've checked the dependencies guide, and all of them appear to be including a binary. I've also checked "The Book", but none of the examples there end in ".rs" either.
How do I make include_me.rs
compile with the rest of the project?
EDIT: while the "possible duplicate" would be a great place to include the answer to this question, it is not inclusive of it. The syntax in the top answer for this question is significantly more relevant, and glossed over in the question it "duplicates"
There are basically two (main) ways in Rust to include code from somewhere else:
1. "Including" internal code
If your
include_me.rs
belongs to your project, you should move it to the same foldermain.rs
lies in:Then you can write this in your
main.rs
:A
mod
declaration makes the Rust compiler look for the corresponding.rs
files automatically!So everything that belongs to your project, belongs in the same folder (or a subfolder thereof) as the folder where
main.rs
(orlib.rs
) is lying. The files are then "included" via the module system. To read a good introduction into modules, please read the chapter on modules in the Rust book. The module system is pretty central and thus important to learning Rust.2. "Including" external code
If your
include_me.rs
is something that doesn't belong to your actual project, but is rather a collection of useful things you are using in multiple projects, it should be seen as a library. To include code of such external libraries, you have to include it as an extern crate. And to make your life easier, you really want to use Cargo!So let's prepare your
include_me.rs
as Cargo library project. You need the following file structure (which is automatically generated bycargo new my_library --lib
):Now copy all the contents from
include_me.rs
intolib.rs
(it is just convention to call the root file of a library projectlib.rs
). Let's say that themy_library
folder's full path is~/code/my_library
.Now let's prepare your main project's Cargo project. It has a similar file structure (but a
main.rs
instead oflib.rs
, because it's a executable-project, not a library-project):To declare your dependency on
my_library
, you have to put this into yourCargo.toml
:You can also use relative paths (
"../my_library"
), but it only makes sense if you know that the two projects always stay where they are, relative to one another (like if they are both managed in the same repository).Now you can, in your
main.rs
, write:If you want to upload any of those two projects, you have to interact with
crates.io
(or another crates registry, if your company has one), but this is another topic.(Note: some time ago, it was necessary to write
extern crate my_library;
insidemain.rs
. This is not necessary anymore, but you might find old cold withextern crate
declarations.)Any other ways?
Yes, but you shouldn't use those. There is the
include!()
macro which allows you to verbatim include other files, just like the#include
from C-land. However, it is strongly discouraged to use this in situations where the module system would be able to solve your problem.include!()
is only useful in very special situations, often linked to a more complex build system which generates code.