#import modules and function

37 messages · Page 1 of 1 (latest)

white pike
#

i have a function inside file_extenion mod called open_csv (mod.rs) how do i import it in table_structure file?
this is my directory tree:

├──  file_extension/
│   ├──   mod.rs
│   └──   table_structure.rs
├──   lib.rs
└──   main.rs
sullen scarab
#

What program produced that second chart? If it's accurate then you have two mistakes here:

  1. You have declared doubly-nested modules. You should not write mod file_extension inside of file_extension.rs; there should only be one mod in your entire project per module.
  2. FileExtension is not pub, so it can't be used outside of the file_extension module. You need to write pub enum FileExtension or pub(crate) enum FileExtension
white pike
white pike
sullen scarab
#

that's the mistake

#

you have a mod file_extension; and a mod file_extension {. that makes you have two modules with the same name, nested

#

you do not declare modules inside of modules.

#

you should have only one mod per module you intend to have. it can be a mod foo; or a mod foo {, but you don't use both

white pike
sullen scarab
#

that's good.

white pike
#

do i remove it?

sullen scarab
#

no, you get rid o the mod file_extension {} that is inside file_extension/mod.rs

#

that is the extra module that you do not need.

white pike
#

but then i wouldn't have a module, right?

sullen scarab
#

no, mod file_extension; declares the module you want to have.

#

the contents of the module go directly in the corresponding file, file_extension/mod.rs

white pike
#

please bare with me i'm new to rust

sullen scarab
#

you do not also re-declare the module inside the file

#

you declare modules only once. not twice.

#

the string mod file_extension should appear in your program only once.

#

you do not need to declare the module once in lib.rs and again inside itself. that just makes two modules.

white pike
sullen scarab
#

when you write mod file_extension;, with a ; instead of a {} you are saying: "please get the contents of this module from file_extension/mod.rs or from file_extension.rs"

#

your source files contain the contents of modules.

white pike
sullen scarab
#

sure

#

(no DMs please.)

white pike
sullen scarab
#

it looks like you did the right thing to file_extension/mod.rs

#

once you have fixed that too, you can do use super::FileExtension; inside table_structure.rs, and be able to use FileExtension::open_csv(). But, actually, maybe that function shouldn't be inside impl FileExtension? It doesn't use FileExtension at all.

#

then the path would be just super::open_csv or crate::file_extension::open_csv

white pike
#

now i understand those mistakes very well, i solved everything and now i'm able to call open_csv just as i wanted

white pike
#

thanks man i appreciate your help