Rust visibility

Created By: chatGPT

In Rust, visibility controls the accessibility of items, such as functions, structs, and modules, within your code. Understanding visibility is crucial for organizing your code and managing its interface.By default, all the items in Rust are private, meaning they are only accessible within the module where they are defined. To make an item accessible from other modules, Rust provides visibility modifiers: pub, pub(crate), and pub(super).
To declare an item as public, use the pub keyword. This allows the item to be used from anywhere in the crate.
pub struct PublicStruct {
    pub field: i32,
}
Using pub(crate) makes an item available throughout the entire crate, but not outside of it.
pub(crate) struct CrateVisibleStruct {
    field: i32,
}
Similarly, pub(super) allows access to items from the parent module. This is useful for sub-modules that need to reference items defined in their parent.
pub(super) struct SuperVisibleStruct {
    field: i32,
}
You can also control visibility at the function level. For instance, by default, functions are private, but you can specify public access as follows:
pub fn public_function() {
    // This function is accessible from anywhere
}
For struct fields, you can also make them individually public by marking each field with pub if you want some fields to be accessible while keeping others private.
pub struct MixedVisibilityStruct {
    pub public_field: i32,
    private_field: i32,
}
Here’s a complete example demonstrating various visibility modifiers:
mod outer {
    pub struct OuterStruct {
        pub inner: InnerStruct,
    }

    mod inner {
        pub struct InnerStruct {
            pub value: i32,
        }
    }
}
In summary, understanding and correctly applying visibility modifiers allows you to create a clear interface for your modules and manage access control effectively within your Rust programs.
Introduction And SetupVariablesData TypesImmutableMutableIntegerFloating PointBooleanCharacterStringArrayTupleVectorSliceHashmapMethodFunctionSyntaxBlock ExpressionIf ExpressionLoopWhile LoopFor LoopMatch ExpressionPattern MatchingOwnershipBorrowingReferencesLifetimesEnumsStructsTraitsImpl BlockGenericType AliasPanicResultOptionError HandlingUnwrappingVariantClosureIteratorAsyncAwaitTrait ObjectModuleCrateAttributeMacroCommentSingle Line CommentMulti Line CommentDoc CommentCargoFormattingOwnership RulesType InferenceShadowingOperatorArithmetic OperatorComparison OperatorLogical OperatorBitwise OperatorAs KeywordConstStaticCopy TraitClone TraitUnsafe CodeFfiCargo ManagementTraits BoundsMatch ArmDerived TraitsClosure CaptureSplit_atIterFilterMapCollectFrom_iterTuple StructUnit TypeNaming ConventionsModule SystemVisibilityPrivatePublicCrate RootUnix Specific FeaturesWindows Specific Features