Visibilty for src/bin/prog.rs when project has src/lib.rs

Hello,

i'm struggling with module visibility. I have a library with a module that i'd like to be accessible only to other programs defined in the same crate. I can only do it if i declare the module/fn as pub. Is it possible to make this more constrained using pub(crate) or pub( in::path) somehow?

As an aside, the errors that i get bring me to the conclusion that files in src/bin are not part of the crate. Is this correct? That makes me wonder if i Should just separate out the library from the executables into separate crates or workspaces.

A minimal example follows.

// src/lib.rs
// want to export module ffi to all programs in src/bin. 
//  pub(crate),pub( ?? ), pub(super) -- this leads to "too may leading `super keywords"
pub(crate) mod ffi {
    pub(crate) fn ffi_fn() { println!("ffi_fn"); }
}

And i'd like to have a binary that can access the module ffi

// src/bin/prog.rs
use lib_vis::ffi;
fn main() {
    ffi::ffi_fn();
}

thanks for any guidance.

The files in ./src/bin/ are treated as entirely seperate crates by Cargo (hence why you have to import via use lib_vis:: rather than use crate::).

You need to make ffi and ffi_fn pub to access them.

1 Like

wow, thank you! This was my hunch, but i also wanted to make sure i wasn't missing something.

Very much appreciate it.

1 Like

No problem!

I think it might have made more sense if Cargo had opted to store binaries in a bin folder rather than a src subdirectory - the former would have felt more consistent with tests/examples, whereas the latter makes it a bit unclear that they're seperate crates IMO. That ship has probably sailed by now, though :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.