Using a crate for utility functions

Well, I thought I had this problem solved. I got some good help on this thread, applied what I learned and everything was working just fine. Still, I had a number of functions that needed to be moved to my utility crate so that I can reuse them (without having to copy/paste them into my code) and, again applying what I had learned, I moved those functions and changed my use statements to reflect that move. Yet the compiler doesn't like it. Sigh. I'm sure it's something simple -- probably just getting the syntax right -- but I need some help finding and correcting it.

The utility crate I'm using is named jt_lib with the code contained in the ../jt_lib/src/lib.rs file. That file contains two modules. Here's what it looks like (in collapsed form):

pub mod misc { ... }

pub mod input_utilities { ... } 

It's the input_utilites{} module that is giving me problems. The compiler is pulling from the misc{} module just fine.

My use statements look like this:

pub mod file_mngmnt {

    use std::{fmt::Debug, fs, fs::File};
    use std::io::{BufRead, BufReader, Write};
    use std::str::FromStr;
    use jt_lib::misc::*;
    use jt_lib::input_utilities::*;

The functions I need to access are contained in the jt_lib::input_utilities::*; module, but the compiler tells me that it can't find them and then warns that the above use jt_lib::input_utilities::*; statement is not needed. Here's the full error message:

camascounty@camas-county:~/programming/rust/mine/file_lib$ cargo build
   Compiling file_lib v0.1.0 (/home/camascounty/programming/rust/mine/file_lib)
error[E0425]: cannot find function `input_string_prompt` in this scope
   --> src/lib.rs:182:21
    |
182 | ...   fname = input_string_prompt("\n Please enter a name for your new ...
    |               ^^^^^^^^^^^^^^^^^^^ not found in this scope
    |
help: consider importing this function
    |
13  + use jt_lib::input_utilities::input_string_prompt;
    |

error[E0425]: cannot find function `input_bool_prompt` in this scope
   --> src/lib.rs:188:30
    |
188 | ...   let choice = input_bool_prompt("\n Do you want to overwrite the f...
    |                    ^^^^^^^^^^^^^^^^^ not found in this scope
    |
help: consider importing this function
    |
13  + use jt_lib::input_utilities::input_bool_prompt;
    |

warning: unused import: `jt_lib::input_utilities::*`
  --> src/lib.rs:21:9
   |
21 |     use jt_lib::input_utilities::*;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0425`.
warning: `file_lib` (lib) generated 1 warning
error: could not compile `file_lib` (lib) due to 2 previous errors; 1 warning emitted

The jt_lib crate is referenced in the dependencies section of my Cargo.toml file:

[dependencies]
# For use in creating menus.
dialoguer = { version = "0.11.0", features = ["completion"] }
console = "0.15.8"
jt_lib = {path = "/home/camascounty/programming/rust/mine/jt_lib/"}

So, any idea what my mistake is?

Your jt_lib::input_utilities::* import is inside mod file_mngmnt { ... }, so it only has an effect directly within that module’s namespace.

The errors are on line 182 and 188. Is this code directly within mod file_mngmnt { ... }, or is it in one of its child modules, or is it outside of that module entirely?

1 Like

You got it. My closing brace } for the module was above the last function in the module, so that function couldn't access the import. I moved the brace and the problem is fixed.

Thanks! :>)