How To Make A Function Public Rust?

Add `pub` before the function name

Example: `pub fn my_function() {}`

For methods inside an `impl`, add `pub` before the method name

Example: `impl MyType { pub fn my_method(&self) {} }`

Make the containing module public if needed

Example: `pub mod my_module { pub fn my_function() {} }`

Make the containing struct fields public if needed

Example: `pub struct MyStruct { pub field: i32 }`

Use `pub(crate)` to make it visible within the current crate

Use `pub(super)` to make it visible to the parent module

Use `pub(in path::to::module)` to restrict visibility to a specific module path

Suggested for You

Trending Today