Brandon Piña
Brandon Piña

Reputation: 893

E0624 when trying to use private methods in a private module in Rust

Let's say I have this module foo with an internal public struct Bar
now, Bar has some functions that should be private, Let's say they're fairly low-level functions that are required to build out some of the bigger, public functions.

let's say we have the following file structure

src/
├─ foo/
│  ├─ private_module.rs // private module with private functions
├─ foo.rs
├─ lib.rs // Public API

within crate::foo::private_module we have the following

impl Bar {
    fn my_private_function() -> () {}
}

and within crate::foo we have the following

mod private_module;

struct Bar;

impl Bar {
    pub fn my_public_function(&self) -> () {
        self.my_private_function() // E0624! private associated function
    }
}

What is the proper way to do this? The only way to get around it is to make these private functions public, which will expose them in the public API of this struct despite the impl block living in a private module!

Should I have the low-level private functions at the root of the module and higher-level public functions in their own submodules?

Upvotes: 0

Views: 128

Answers (1)

pigeonhands
pigeonhands

Reputation: 3424

The Visibility and Privacy docs has a number of different visibility levels you can use, such as pub(super) to only expose items to its parent module.

struct Bar;

impl Bar {
    pub(super) fn my_private_function() -> () {}
}

Upvotes: 2

Related Questions