Open In App

Rust – Struct Visibility

Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, there is a concept of Struct Visibility. Generally, the items present in the module have visibility that is private by nature but we can override the private items by usage of the public (‘pub’) modifier. Public items of the module are accessed outside the module scope and the visibility in these scenarios is not very high. To avoid this situation and to enable an extra layer of visibility, Structs are introduced in Rust. 

Usage of Structs helps the developers to design their code as per requirements i.e. depending on the scenario we can make functions public or private and in case the struct is private, it can be modified to public struct by using ‘pub’. Rust generally enforces encapsulation of data, by introducing private visibility in situations when we are accessing the fields in the struct outside the defined modules.

Accessing Public Defined Struct:

Example 1:

Rust




// Accessing Public Defined Struct in Rust
mod geeks_for_geeks {
    pub struct GfgWebsite<T> {
        pub courses: T,
    }
    #[allow(dead_code)]
    pub struct GfgCourses<T> {
        courses: T,
    }
  
    impl<T> GfgCourses<T> {
        pub fn new(courses: T) -> GfgCourses<T> {
            GfgCourses {
                courses: courses,
            }
        }
    }
}
  
fn main() {
    let _gfg_one = geeks_for_geeks::GfgWebsite 
  { courses: "DSA courses are currently on sale" };
    println!("{}", _gfg_one.courses);
  
}


Output:

 

Explanation:

In this example, we have firstly declared module geeks_for_geeks which is a struct with a public field module and a generic type of T is declared. We have defined an struct GfgWebsite which is public and also has a type T. In the struct GfgWebsite, there is also courses type that is defined public. Next, we define a struct GgfCourses that has an private field courses that also has type T. Next, we implement a constructor named ‘new’ that is implemented on the GfgCourses function. It has courses as its private attribute.

Now, we declare a public struct with an public field named _gfg_one and assign the module::function field in it. Here, the module is geeks_for_geeks and the function is GfgWebsite. _gfg_one holds the value of geeks_for_geeks::GfgWebsite and a string is passed to the courses field. In this case, the public structs that has public fields can be constructed as well as accessed normally. After printing, we can see the output on screen.

Accessing Private Constructor Field:

Example 2:

Rust




// Accessing Private Constructor Field in Rust
mod geeks_for_geeks {
    #[allow(dead_code)]
    pub struct GfgCourses<T> {
        courses: T,
    }
  
    impl<T> GfgCourses<T> {
        // new is a public constructor
        pub fn new(courses: T) -> GfgCourses<T> {
            GfgCourses {
                courses: courses,
            }
        }
    }
}
  
fn main() {
    let _gfg_two = geeks_for_geeks::GfgCourses::new
  ("Doubt support is enabled on Gfgcourses");
     println!("{}", _gfg_two.courses);  
}


Output:

 

Explanation:

Continuing from scenario 1, we have now declared a variable _gfg_two. In Rust, the structs having private fields are created using public constructors and private fields of public structs is not accessible i.e. here GfgCourses is an public struct that has an public field(courses). The courses field is private (via encapsulation) so if we try to access constructor field new by passing it through gfg_two then it will throw an error as private fields of  public structs is not accessible. 



Last Updated : 19 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads