I have three files under the src
directory:
src
|____ keys.rs // a 'sub-module' under `lib.rs`
|____ lib.rs
|____ main.rs // binary file
In my cargo.toml
, I specify specifically the paths to the lib
and bin
, i.e.:
[lib]
name = "lib"
path = "src/lib.rs"
[[bin]]
name = "urs" // name of my binary
path = "src/main.rs"
In keys.rs
, I defined several structs and functions.
In lib.rs
, I declare mod keys;
, i.e.:
pub(crate) mod keys;
use [...]
In main.rs
, I want to use the module keys
from lib
, so I do:
use lib::keys;
use [...]
I want to expose the module keys
to be used anywhere within the crate but disallow its use outside of it, hence, I use the visibility modifier pub(crate)
when declaring pub(crate) mod keys;
in lib.rs
.
However, cargo is telling me this:
error[E0603]: module `keys` is private
--> src/main.rs:5:10
|
5 | use lib::keys;
| ^^^^ private module
|
note: the module `keys` is defined here
--> F:\rust_projects\sui-utils-rust\src/lib.rs:1:1
|
1 | pub(crate) mod keys;
| ^^^^^^^^^^^^^^^^^^^
My questions:
Is it possible to achieve what I intend to achieve? I.e. by declaring pub mod keys;
I'll be allowing external crates to use stuff within the keys
module, how can I prevent that while retaining crate-exclusive access to the keys
module?
keys
public, and add//! #[doc(hidden)]
to the top ofkeys.rs
so that at least it won't appear in the docs, which should discourage others from using it. See doc.rust-lang.org/rustdoc/write-documentation/…