Module-private visibility


I’m currently learning rust and so I’m trying to do a little game.

For this I have a module named models that contains several file:


That make sense to me that some struct field and function have to be public in the module (for collision computation as example) but I didn’t want to provide an access to them outside of the module.

First question, is there a way to publish some struct field inside models but not outside of it ?

My struct player, enemy and ball implement the same trait for updating and rendering. And I only want to publish these function plus some more to handle player input. As for the struct field can I apply a similar strategy for the function inside an impl block ?

You can do that. Take a look at Visibility and privacy - The Rust Reference for your options. You can use pub (super) ... in the child modules of the models module or use pub (in path::to::models) if you want to be somewhat agnostic of how models’ submodules are organized.

1 Like