-1

Although there are some questions related to this but all seems confusing to me.

I have a project with below structure.

When import cli module in main.rs it works properly but when I tried to do same in lib.rs I get below error

  = help: name the file either lib/cli.rs or lib/cli/mod.rs inside the directory "src"

but I want to keep it inside cli folder because I will have to add more code in cli folder. why is cargo asking me to create lib folder ?

below is my toml file.

[package]
name = "feline"
version = "0.0.1"
license = "MIT"
authors = ["Akshay Naik <inbox.akshaynaik@gmail.com>"]
edition = "2018"

[dependencies]
smol = { version = "0.1", features = ["tokio02"] }
futures = "0.3.4"
clap = "~2.27.0"

Update: Thea goal is move all logic to lib.rs and call single function from main.rs

Code:

https://github.com/nakshay/feline/tree/lib-use/src

Akshay Naik
  • 669
  • 1
  • 6
  • 22
  • The lib folder is a module, and lib/cli is a submodule of lib. So in the lib/mod.rs file you should export the cli module, or when you want to acces cli, you should use lib::cli::whatever. – Mario Santini May 12 '20 at 11:17
  • Relevant questions: https://stackoverflow.com/questions/48071513/how-to-use-one-module-from-another-module-in-a-rust-cargo-project https://stackoverflow.com/questions/26388861/how-to-include-a-module-from-another-file-from-the-same-project https://stackoverflow.com/questions/30670129/access-submodule-from-another-submodule-when-both-submodules-are-in-the-same-mai – E_net4 May 12 '20 at 11:19
  • @MarioSantini It works from main.rs and I have definitely exported it in mod.rs, you can see full source at https://github.com/nakshay/feline – Akshay Naik May 12 '20 at 11:24
  • @MarioSantini, I am trying to do same thing which is shown in rust book cli chapter, creating minigrep, 1st link is irrelevant since I am already exporting modules and 2nd link is having different directory structure. – Akshay Naik May 12 '20 at 12:27
  • In short my question is why can't I import modules in lib.rs similar to main.rs, any doc or blog explaining that is appreciated – Akshay Naik May 12 '20 at 12:29
  • Please endeavor to create a [mre]. You seem to have shown everything *except* the code that demonstrates the actual problem. The dependencies and extra files seem to be unnecessary here; please try to strip out extraneous details to help us answer you better. – trent May 12 '20 at 14:01
  • Whatever you put in `main.rs` as far as `mod`/`use` should also work in `lib.rs`; they are treated as independent crate roots. So if you have `mod cli;` in `main.rs` you should also put `mod cli;` in `lib.rs`. Alternatively you can put `mod cli;` in `lib.rs` and in `main.rs` you use the library crate, which is named with the project name: `use feline::cli;` Also read [Why do I need `use` statements in a submodule but not in main.rs?](https://stackoverflow.com/q/52265867/3650362) (and Q&As linked from there) for more on the module system. – trent May 12 '20 at 14:09
  • @trentcl, you will get complete code at this path https://github.com/nakshay/feline/tree/lib-use I have added everything as you said already, let me know if I miss anything – Akshay Naik May 12 '20 at 14:42
  • I also checked if I have two files file1.rs --> fn function_one(){..} and file2.rs --> fn fuction_two(){} along with main.rs I can not say write mod file2 inside file1.rs, cargo says create file file1/file2.rs of file1/file2/mod.rs – Akshay Naik May 12 '20 at 15:39

1 Answers1

3

removing line mod lib; and calling feline::start() solved the issue,

I got my answer on rustlang forum. the line.

"When you use the mod keyword, you are creating a module, not importing a module" was an eye opener.

see the full answer by visiting below link.

https://users.rust-lang.org/t/main-rs-and-lib-rs-at-same-level/42499

Akshay Naik
  • 669
  • 1
  • 6
  • 22