9951 explained code solutions for 126 technologies


rustRust lang class property


Class properties in Rust are defined using the pub keyword. This keyword makes the property accessible from outside the class. The syntax for defining a class property is as follows:

pub property_name: type

For example, if we want to define a class property called name of type String, we can do so as follows:

pub name: String

The following is an example of a class with a property called name of type String:

pub struct Person {
    pub name: String,
}

fn main() {
    let person = Person {
        name: String::from("John"),
    };

    println!("Name: {}", person.name);
}

Output

Name: John

Explanation:

  • pub: This keyword makes the property accessible from outside the class.
  • name: This is the name of the property.
  • String: This is the type of the property.
  • Person: This is the name of the class.
  • let person = Person { name: String::from("John") };: This line creates an instance of the Person class and sets the name property to John.
  • println!("Name: {}", person.name);: This line prints the value of the name property.

Helpful links:

Edit this code on GitHub