How can I access the binary crate inside the library crate?

Hello,

I did a search on SO and found this https://stackoverflow.com/questions/26946646/package-with-both-a-library-and-a-binary but it does not address my question. Neither do the suggested questions here.
I have a project consisting of a lib.rs crate and a main.rs crate with a module VM in main.rs. I want to write unit tests for the VM module and to set up all the structs needed for testing I want to write a setup_vm() function in lib.rs where all the utility functions should reside in.
I do not know how to access main::VM inside lib.rs.

Thanks!

You can't. Library crates can't depend on binary crates.

1 Like

The general recommendation is typically to have most of your code in the library, e.g. lib.rs, and have minimal binary files (main.rs or other apps in the bins folder) which calls your library. So you can apply integration test for your library code, and additional use unit test, which can even access private functions. For testing if the tiny main.rs can actually run, you can use an own launched process if you think that is necessary. That was recently discussed in this forum.

1 Like

Thanks!