Managing Function Visibility in RustšŸ¦€ with "pub": A Simple Guide

Abinash Sahoo - Oct 2 - - Dev Community

In Rust, controlling who can access your functions is important for writing clean, secure code. Most developers know about pub and private functions, but letā€™s take a step further and explore more advanced visibility options.

Here is the link to the full code. Click here!


Basic Visibility: Public and Private Functions

At the simplest level, functions in Rust are either public or private:

fn private_function() {
    println!("Private function");
}

pub fn public_function() {
    println!("Public function");
}
Enter fullscreen mode Exit fullscreen mode
  • Private functions are the default. They are only visible within the same module in which they are defined.
  • Public functions are made public using pub, making them accessible anywhere in your crate or by other crates (if used in a library).

This is where most developers start. You can make a function public if you need to share it or keep it private for internal logic. But what if you need more fine-grained control? Thatā€™s where advanced pub options come in! šŸš€


Advanced Functions Visibility: Getting Granular

Sometimes, you donā€™t want to make a function completely public. You just want it available in certain parts of your code without exposing it everywhere. Letā€™s look at how Rust gives you this control.

1. Public within a specific module

pub(in crate::my_mod) fn public_function_in_my_mod() {
    println!("Visible only inside my_mod");
}
Enter fullscreen mode Exit fullscreen mode

šŸ“Œ What it does: This function is visible only within the my_mod module. Outside of it, the function remains inaccessible.

šŸ” Use case: When you have a helper function that should only be used within a specific module and should not be exposed to other parts of the codebase.

2. Public to the parent module

pub(super) fn public_function_in_super_mod() {
    println!("Visible to the parent module");
}
Enter fullscreen mode Exit fullscreen mode

šŸ“Œ What it does: This makes the function visible only to the parent module.

šŸ” Use case: If the parent module needs access to a childā€™s function without exposing that function more broadly, this is a great option. It keeps your code organized and reduces unwanted access.

3. Public within the entire crate

pub(crate) fn public_function_in_crate() {
    println!("Visible within the crate");
}
Enter fullscreen mode Exit fullscreen mode

šŸ“Œ What it does: The function is accessible anywhere within the crate but is still hidden from other crates.

šŸ” Use case: Use this when you want all modules in your crate to have access to the function but donā€™t want to expose it outside the crate. This is often useful for internal functionality that shouldnā€™t be part of your public API.


Why Does This Matter?

Rustā€™s advanced pub keyword allows you to limit access to your functions precisely, which:

  • Protects internal code from accidental misuse by other modules or developers.
  • Keeps your code secure by limiting what is exposed to external crates.
  • Helps you write organized code by giving access only where needed.

Letā€™s say you have a complex project with several modules. If you expose too much, other parts of your code might depend on internal logic, making it harder to maintain.

On the other hand, hiding everything might make the code less reusable. Rustā€™s pub keyword options give you the perfect balance!


Conclusion

By using the pub(in path), pub(super), and pub(crate) options, you can:

  • Control exactly who can use your functions.
  • Keep internal logic private while allowing specific modules or crates to access the necessary functionality.
  • Write cleaner, more maintainable code.

Ready to Try It?

Give these pub visibility options a try in your next Rust project! šŸŽÆ Youā€™ll be surprised at how much more flexible and organized your code can be. Got questions? Drop a comment below, and letā€™s chat! šŸ˜Š

Have a great day.

Happy Rust Journey!šŸ¦€

. . . . . . . . . . . .
Terabox Video Player