Using Rust lib functions in module

is_sys_path() is in lib.rs
I'm trying to use it in utils.rs

I see Using Rust modules and libraries

but what about the reverse, using Rust library functions in a Rust pub mod ?

From my understanding, this should just kind of work via crate::lib_fn() or maybe my_lib::lib_fn() but I'm getting:

cannot find function 'is_sys_path' in module 'crate'

or

failed to resolve: use of undeclared crate or module 'my_lib'

cargo.toml

[package]
name = "my_lib"
version = "0.1.1"
authors = ["mike"]
publish = false
edition = "2018"

[dependencies]
...
[lib]
name = "my_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

File Tree

- src
  - utils.rs
  - metrics.rs
  - lib.rs
  - main.rs
  - fv_structopt.rs
  - fv_structs.rs
- cargo.toml

utils.rs


pub mod utils {
    use crate::metrics::metrics as metric;

    fn get_dir_deep_check(deep: bool, dir_entry_path: PathBuf) -> bool {
        file_vacuum::is_sys_path();  // error
        let is_not_sys = !crate::is_sys_path(&dir_entry_path); // error

    ...
}

lib.rs

pub mod metrics;
pub mod utils;

use utils::utils as util;
use metrics::metrics as metric;

pub fn is_sys_path(path: PathBuf)  {
   ...
}

main.rs

mod fv_structopt;
mod fv_structs;

...

Show us your lib.rs and main.rs

Added lib.rs

Nothing notable like

// mod lib;
// extern crate file_vacuum_lib;

In main.rs

You don't have any mod statements in lib.rs or main.rs?

@alice updated lib.rs and main.rs at the top :point_up:

So one thing is that you typically would not have a mod utils { ... } inside the utils.rs folder, because then you have an utils module inside an utils module. But regarding the error, I'm guessing that you may have a mod utils in main.rs as well?

@alice just the two mods in main.rs above.

Can you post the full error message as well?

use utils::utils as util;
use metrics::metrics as metric;

Yeah, this is what happens when you put a mod utils { .. } block inside utils.rs. Just remove the block and you don't need these.

1 Like

This sounds like it could be the solution. I'll try it out and mark it if so.

@alice what's src/utils.rs for then? imports?

Huh? The src/utils.rs file should just look like this:

use crate::metrics::metrics as metric;

fn get_dir_deep_check(deep: bool, dir_entry_path: PathBuf) -> bool {
    file_vacuum::is_sys_path();  // error
    let is_not_sys = !crate::is_sys_path(&dir_entry_path); // error

...

without the pub mod utils { ... } part

1 Like