Re:Rust from a Gopher – Lessons 7, 8 and 9
by kd5bjo on Wednesday November 18, 2020 @10:05AM
(#25137522)
> If `mod foo;` is the only way to make sure a file's code gets compiled, then at some point every file gets its own module, right?
There’s also the `include!` macro which can read a source file without making it a module and the `#[path=...]` directive which can let you use the same source file for several modules.
Re:Rust from a Gopher – Lessons 7, 8 and 9
by pornel on Wednesday November 18, 2020 @06:38PM
(#25144184)
If you want to, you can easily make a project comprised of many modules and using only one file (or zero files)
Creating a single module from many files is also possible. It can be done with clumsy C-style includes, or more idiomatically composed from `pub use` of items from private modules. `impl` blocks can be anywhere.
The point is, files and modules are decoupled. Files just happen to be a convenient default for modules, but they aren't semantically special in Rust. There's no syntax or privacy boundary specific to files.
) Cargo.toml with `[lib] path = "/dev/stdin"` + `echo 'fn main() {}' | cargo build` happens to work :)
Every file is a module, but not every module is a file.
If `mod foo;` is the only way to make sure a file's code gets compiled, then at some point every file gets its own module, right?