Learn Before
Code
Example of Struct - Rust Programming
// Define a struct named `Person` // with three fields: `first_name`, // `last_name`, and `age`. struct Person { first_name: String, last_name: String, age: u32, } // Implement a method for the `Person` struct. impl Person { fn full_name(&self) -> String { format!("{} {}", self.first_name, self.last_name) } } fn main() { // Create a new instance of the `Person` struct. let person = Person { first_name: String::from("John"), last_name: String::from("Doe"), age: 30, }; // Access the fields of the `Person` struct. println!("First Name: {}", person.first_name); println!("Last Name: {}", person.last_name); println!("Age: {}", person.age); // Call the method on the `Person` instance. let full_name = person.full_name(); println!("Full Name: {}", full_name); }
0
1
Updated 2023-04-20
Tags
Object-Oriented Programming
Programming Language Paradigms
General programming languages
Computing Sciences