Using another_file
with underscore in as word separators works fine in Rust.
How do i use hyphens instead (another-file.rs
)?
// another-file.rs
pub fn method() { }
// lib.rs
use another_file; // <-- ERROR can not find another_file.rs
another_file::method();
You need to make it public if you want it callable outside the sub module: pub fn sub() {...} . As for rustc, you can run cargo with a verbose flag (e.g. cargo build -v ... ) and then look at how it invokes rustc . error: Could not compile hello . To learn more, run the command again with --verbose.
Rust works with modules, as part of a module tree. There is no such thing as file imports.
To import the module, we use the keyword mod , followed by the file name without the . rs extension. When a module is imported, it allows us to use any code that's marked as public in the file we're importing the module from. Note that the module must be imported before we use any of its code.
In Rust 2018, mod.rs is no longer needed. This eliminates the special name, and if you have a bunch of files open in your editor, you can clearly see their names, instead of having a bunch of tabs named mod.rs.
You will have to explicitly declare the module and provide its path:
#[path="../path/to/another-file.rs"]
mod another_file;
use another_file;
However this is not common, and I wouldn't recommand it. Just stick with snake_case for module names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With