Inner Modules?

forkit forkit at gmail.com
Sun Feb 13 00:32:51 UTC 2022


On Saturday, 12 February 2022 at 22:51:58 UTC, forkit wrote:
>

As a demonstration:

.. the superpower of Rust's inner modules (even when using 
'unsafe'):

// ---

#![allow(non_snake_case)] // damn those language designers!

fn main() {
     unsafe {
         innerModule::SIZE = 10; // compile time error -> static 
`SIZE` is private
     }
}

// Remember, private is default in Rust.
// So both this innerMod and the static var SIZE are both private.
//
mod innerModule {
     // annotate below pub to make it accessible outside this 
inner module.
     static mut SIZE: i32 = 5;
}

// -----

More very interesting analysis here:

https://iximiuz.com/en/posts/rust-privacy-and-visibility/



More information about the Digitalmars-d mailing list