Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "use another-file" in rust? Module with hyphens in it

Tags:

rust

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();
like image 674
amin Avatar asked Aug 17 '19 09:08

amin


People also ask

How do you call a function from another file in Rust?

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.

Is a file a module in Rust?

Rust works with modules, as part of a module tree. There is no such thing as file imports.

How do I import a rust file?

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.

Are Runescape Mods deprecated?

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.


Video Answer


1 Answers

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.

like image 158
mcarton Avatar answered Nov 14 '22 00:11

mcarton