Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is using same modules in different files efficient (Rust)?

let’s say i have two files

file1.rs
file2.rs

in file1.rs:
I use module:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

use abc;

similarly in file2.rs
I use module:

use abc;

is it memory efficient or time effecient to do that? or should I merge code of file2.rs into file1.rs? to save import time/memory? i want to follow above approach to make my code more readable and manageable.

Thank you!

>Solution :

I think you misunderstand. Rust isn’t an interpreted language. It is a compiled language.

That means, there is absolutely zero overhead in importing as many things as you want. It will slow down the compiler that creates your program (although even that will only get noticable if you import hundrets of things, maybe even thousands). But your final executable has no notion of .rs files any more, it’s all a big bunch of binary, optimized processor instructions.

You can use a file 1000 times in different modules, it will most likely still result in only one single instance in the final executable. (or multiple, in the case of generics, but the amount of use statements still doesn’t matter)

To demonstrate: You can compile your program and then delete all the source files. It will still run.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading