Publisher Theme
Art is not a luxury, but a necessity.

Rust Modules Confusion When There Is Main Rs And Lib Rs Stack Overflow

Rust Modules Confusion When There Is Main Rs And Lib Rs Stack Overflow
Rust Modules Confusion When There Is Main Rs And Lib Rs Stack Overflow

Rust Modules Confusion When There Is Main Rs And Lib Rs Stack Overflow 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. When creating a project with a lib.rs and a main.rs, you should not declare lib as a module in your main.rs file. you should instead just call feline::start() in your main function: feline::start();.

Rust Use Of Main Rs And Lib Rs In Single Project Can Not Import
Rust Use Of Main Rs And Lib Rs In Single Project Can Not Import

Rust Use Of Main Rs And Lib Rs In Single Project Can Not Import A common pattern, even for binary only crates, is to declare a "library" providing the majority of functionality, and then have main.rs just implement argument parsing and use that library. Rust provides a powerful module system that can be used to hierarchically split code in logical units (modules), and manage visibility (public private) between them. I read through the docs, and the changes from rust 2015 to 2018, which seems to confirm what i'm thinking. i've never seen this explicitly said, though: main.rs (and maybe lib.rs) act differently from all other files in a package crate. But because we needed to write some unit tests, we decided to also enable a cli esque functionality via adding a main.rs. the problem now is that the main.rs exists, rust analyzer thinks main.rs has priority over lib.rs and there is a lot of unused code errors showing.

Main Rs And Lib Rs At Same Level Help The Rust Programming Language
Main Rs And Lib Rs At Same Level Help The Rust Programming Language

Main Rs And Lib Rs At Same Level Help The Rust Programming Language I read through the docs, and the changes from rust 2015 to 2018, which seems to confirm what i'm thinking. i've never seen this explicitly said, though: main.rs (and maybe lib.rs) act differently from all other files in a package crate. But because we needed to write some unit tests, we decided to also enable a cli esque functionality via adding a main.rs. the problem now is that the main.rs exists, rust analyzer thinks main.rs has priority over lib.rs and there is a lot of unused code errors showing. That's because both lib.rs and main.rs will read module files from the same directory, so it's already tricky to recognize which file is used by which build. it's a messy situation even the files aren't used twice. Here, we have a package that only contains src main.rs, meaning it only contains a binary crate named my project. if a package contains src main.rs and src lib.rs, it has two crates: a library and a binary, both with the same name as the package. I created this example repository with some quick changes to project structure and code (mainly focusing on main.rs) to show some of the ideas i mentioned above. To people coming from languages like java and go, main.rs and config.rs being in the same folder suggests they can be part of the same module, but this isn't true in rust, where you have to use sub modules.

How To Define A Separate Lib Rs In The Same Folder With Main Rs Help
How To Define A Separate Lib Rs In The Same Folder With Main Rs Help

How To Define A Separate Lib Rs In The Same Folder With Main Rs Help That's because both lib.rs and main.rs will read module files from the same directory, so it's already tricky to recognize which file is used by which build. it's a messy situation even the files aren't used twice. Here, we have a package that only contains src main.rs, meaning it only contains a binary crate named my project. if a package contains src main.rs and src lib.rs, it has two crates: a library and a binary, both with the same name as the package. I created this example repository with some quick changes to project structure and code (mainly focusing on main.rs) to show some of the ideas i mentioned above. To people coming from languages like java and go, main.rs and config.rs being in the same folder suggests they can be part of the same module, but this isn't true in rust, where you have to use sub modules.

Comments are closed.