Object in Rust

Overview

In Rust, an object is an instance of a type, such as a struct or enum, that represents state. Objects in Rust can have methods, and calling these methods uses syntax similar to that of object-oriented (OO) languages, such as object.method(). However, unlike traditional OO languages, Rust does not support inheritance. This design choice emphasizes composition over inheritance, encouraging developers to build complex functionality by combining simpler components.

Object-Member Visibility and Access

In Rust, the members of objects (specifically structs) are private by default. This means that they cannot be accessed directly from outside the module in which they are defined. To make a member accessible from outside its module, you can use the pub keyword to make it public. However, it is generally recommended to use accessors (getter methods) and mutators (setter methods) to provide controlled access and modification of object members. This approach allows for encapsulation, ensuring that the internal state of an object can only be changed in well-defined ways.