You have multiple options here:
-
#[path = "../utilities.rs"] mod utilities;
And then use
utilities::item
to refer to someitem
defined (pub
) insideutilities.rs
.- More generally,
crate::utilities::item
if within a submodule insidehowto-...
.
- More generally,
-
include!("../utilities.rs");
This is the less Rust-idiomatic solution, but may be the closest one to fit your C mental model.
I do not recommend using it in the long term, since you lose namespacing, as well as any privacy features from
utilities.rs
itself, but may be a good starting point.Once you are comfortable using
include!("../utilities.rs");
, you can follow up with namespacing:mod utilities { include!("../utilities.rs"); }
- and have all the items in
utilities.rs
be markedpub
.
This will be the same as the initial basic
include!
, but now you need thatutilities::
namespace prefix to refer to the items of that fileAt that point, you can toy with removing some
pub
-annotations insideutilities.rs
, if you so wish, and see how the "namespace" is also a privacy boundary: that's what amod
ule is.And once you have figured that out, just know that
[path = "../utilities.rs"] mod utilities;
is the idiomatic short-hand equivalent of:
mod utilities { include!("../utilities.rs"); }
-
In this case, not that much of a shorthand since you had to provide an explicit
path
annotation due to the circumstances, but in general,mod some_name;
will be equivalent to:
-
either
mod modname { include!("modname.rs"); }
-
or
mod modname { include!("modname/mod.rs"); }
(chosen by Rust depending on which file is actually present).
-
- and have all the items in
-
The third option involves
Cargo
, and the layout of dualbin/lib
packages (i.e., a package (a "project" of sorts)) that features both a library crate and one or more binaries (binary crates) that depend on that ownlib
as if it were an external one.That is, if your
Cargo.toml
is of the form:[package] name = "some_name" ...
Then starting at
lib.rs
(but you can override that default "root path"), a library crate namedsome_name
will be defined, and you will be able to refer to its items from within yourhowto-...
crates as with the item of any other external crate (within the 2018 edition, which has been the default one for a while now, so you can ignore / dismiss this remark):::external_crate_name::item
, that is, here,::some_name::item
(from the[package]
name, remember).It also happens that the Rustaceans are too lazy to type a leading
::
, so you can even skip writing it (leading tosome_name::item
), although, if you are starting, I recommend you don't do that until you have clearly understood how the Rust item paths work and how they are resolved.This means that in your case, you could edit your
Cargo.toml
and add:[lib] name = "utilities" path = "src/utilities"
which will override both the default root path for that library (instead of the default
src/lib.rs
), and the name used to refer to its items (::utilities
instead of::some_name
)This means that from within
src/bin/*.rs
you will be able to refer to the items defined inutilities.rs
by using::utilities::item
, without needing to add anything to thosesrc/bin/*.rs
files whatsoever