Search code examples
modulerustprivatepublic

Split implementation across multiple files/modules and keep everything as private as possible


Consider the following code in my library:

pub struct Foo;
impl Foo {
    fn helper(&self) { /* .. */ }
}

pub fn do_something(foo: &Foo) {
    foo.helper();
}

The users of my library should be able to use Foo and do_something(), but shall never call helper(). Everything works fine with the code above. But now imagine the code gets huge and I want to separate those definitions into their own files/modules -- they are then pulled back into the root-namespace with pub use. See here:

mod foo {  // module contents in `src/foo.rs`
    pub struct Foo;
    impl Foo {
        fn helper(&self) { /* .. */ }
    }    
}

mod do_something {  // module contents in `src/do_something.rs`
    use super::Foo;
    pub fn do_something(foo: &Foo) {
        foo.helper();
    }
}

pub use foo::Foo;
pub use do_something::do_something;

Now the compiler complains that helper() is private. Indeed it is, but I still want do_something() be able to use it. But I don't want my users to use it. How can I do that?

My attempted solution boils down to "How to access private items of sibling modules?". So answering that question would help me. But providing an alternative solution/a workaround would also be great!


Solution

  • As of Rust 1.18, you can use the pub(crate) syntax to expose something throughout your entire crate but not outside of it:

    mod foo {  // module contents in `src/foo.rs`
        pub struct Foo;
        impl Foo {
            pub(crate) fn helper(&self) { /* .. */ }
        }    
    }
    

    All the possibilities, as suggested by the compiler, include:

    • pub(crate): visible only on the current crate
    • pub(super): visible only in the current module's parent
    • pub(in path::to::module): visible only on the specified path