Coming from primarily C world, i still struggle a bit with what is the idiomatic way to separate, and "include"/use code in various parts of program.
Thanks to extensive and very helpful response i got in another thread in the past, i am sort of able to "include" code when/as needed.
However, it's still unclear to me e.g. what is the recommended way to share some code that is rather frequently used in many various application files.
The Rust book examples to me feel rather insufficient, showing how to write several mods into single file main.rs. This IMHO misses the primary point of separating the code into smaller parts, that it think of as files, not sections of same file (coming from C waters).
/src/
main.rs
myutils.rs
some_ui.rs
other_ui.rs
...
myutils.rs may e.g. contain 10 constants specific to generic UI design, and making it a standalone "lib" feels like overkill, otherwise the correct approach would be rather clear...
Trying something tiny bit more complex in real world, i immediately fail - is there a specific reason why mod myutils;
statement works only in main.rs
, but not in any other file that is sibling of main.rs
.
It looks like i have to either:
-
drag all the minor stuff through the main.rs file and mod statement there, to allow crate:: prefix to work in other files for mod statement/inclusion
-
use two/three lines per each "file" include (#[path], mod, use) i want to use in my files (some_ui, other_ui, etc_ui....); this feels very verbose (compared to single line of C's
#include "./myconsts.h"
(of course, the difference in raw C include vs namespacing etc. yet, still, it feels lik, the verbosity, with, possibly, some magic...)
What is the recommended pattern for similar code shared in multiple parts of the codebase?