How to reference private types from public functions in private modules?

I have a feeling that this error is unwarranted. It should only be an error if my_mod is pub or if the functions are re-exported in an outer scope.

That said, I found a workaround: move MyStruct to a sibling module and make MyStruct pub, but not the module.

use types::MyStruct;

mod types {
    pub struct MyStruct;
}

mod my_mod {
    use super::MyStruct;

    pub fn foo() -> MyStruct {
        MyStruct
    }
}

fn main() {
    let _var = my_mod::foo();
}

Leave a Comment