You have multiple options here:
-
#[path = "../utilities.rs"] mod utilities;And then use
utilities::itemto refer to someitemdefined (pub) insideutilities.rs.- More generally,
crate::utilities::itemif 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.rsitself, 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.rsbe 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 file
At 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 amodule 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
pathannotation 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/libpackages (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 ownlibas if it were an external one.That is, if your
Cargo.tomlis of the form:[package] name = "some_name" ...Then starting at
lib.rs(but you can override that default "root path"), a library crate namedsome_namewill 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.tomland 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 (::utilitiesinstead of::some_name)This means that from within
src/bin/*.rsyou will be able to refer to the items defined inutilities.rsby using::utilities::item, without needing to add anything to thosesrc/bin/*.rsfiles whatsoever