2

Many times throughout the Rust documentation, examples pop up which use lib.rs files. After some research, I understand that it is common practice in the rust community to use main.rs to point to the lib.rs file, which would house most of the program's functionality.

My issue is that I haven't been able to find any resources which describe how to connect the lib.rs to main.rs.

How do I expose lib.rs to main.rs?

Gunty
  • 1,841
  • 1
  • 15
  • 27
  • What do you mean by "workflow" and "process" here ? Isn't it enough to have a look at a small example project having both files ? (I'm not disqualifying your question, just trying to clarify it) – Denys Séguret Nov 24 '21 at 07:37
  • a small example project would be excellent. The issue is that I can't immediately find one by searching around. Hence, it makes sense to create post asking for direction to one. For sure I could keep looking for one, but by posting, future people who also wish find an example of this can easily do so through this post. – Gunty Nov 24 '21 at 07:40
  • 1
    Here's a small example: [rhit](https://github.com/Canop/rhit/tree/main/src), which has both files. There's nothing more in the workflow than ensuring both files are here. – Denys Séguret Nov 24 '21 at 07:40
  • by "workflow" and "process", i mean: how do they write the code that allows them to point `main.rs` to `lib.rs`? do they have to import crates with `use`? Do they have to use `pub` to expose their structs and functions? etc. hope that helps – Gunty Nov 24 '21 at 07:43

2 Answers2

4

The workflow is pretty simple as a project is defined in Rust with only a few files.

When your project has a src/lib.rs file, then it's a library and can be called by other crates.

When your project has a src/main.rs file, then it's built into an executable.

And your project can have both, which simplifies exposing functions of your application for other crates.

Here's a small example: rhit, which has both files.

As simple organization :

  • declare all modules and pub use in the lib.rs
  • have a main function in your main.rs, running the application

When your core features are more important and may be used in another application, or when the application needs dependencies which are useless for other applications needing your core features, then the alternative should be considered: separate your application in two crates witch clearly distinct roles. You may find this for example in lfs wich uses lfs-core, the latter one being available in other applications which won't import the specific dependencies of lfs.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

This post here has responses which discuss at it's simplest how to bring stuff from lib.rs into main.rs.

Rust modules confusion when there is main.rs and lib.rs

The key thing that I felt connected the dots was the explanation by L.Y Sim that you needed to reference the project's name to expose lib.rs to main.rs.

One more thing, if you wanted to access items that are exposed in src/lib.rs from src/main.rs, you have to do as @zrzka said, which is to name the name of the crate that both src/lib.rs and src/main.rs share. For example, in your project which is named hello-world:

use hello_world::foo;
fn main() {
    foo::say_foo();
}
Gunty
  • 1,841
  • 1
  • 15
  • 27