Answers for "struct in rust"

3

rust struct

struct Data {
	randomInfo: String
}

let mut Data {
	randomInfo: "This is a structure"
}

/* 
 The mut keyword defines whether or not you can mutate (change)
 the variable (in this case a structure), so if you don't want tp be able to 
 change the data then don't use the mut keyword. Like so:
*/

let Data {
	randomInfo: "This is a structure"
}
Posted by: Guest on January-08-2022
0

struct in rust

// as example a for a person
struct Person {
    pub name    : String,   // public
    age     : u8,           // private
    // here are all the attributes and there types
}
// impl contains all functions and methods that will be implemented to your struct
impl Person {
    // a function from the class itself
    fn new(name:String, age:u8) -> Self {
      	// crate an instace of the class
        Person {
            name    : name,
            age     : age
        }
    }
    // a method from an object of this class
    fn get_age(self) -> u8 {
        // `self` is the object itself and the same like `this` in java
        self.age
    }
}
Posted by: Guest on March-30-2022

Browse Popular Code Answers by Language