struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rectangle1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rectangle1.area()
);
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
This program first defines a struct called Rectangle with two fields: width and height. Then, it defines a function called main which creates a new instance of the Rectangle struct and calls the area method on it. The area method calculates the area of the rectangle and returns it as a u32 value. Finally, the main function prints the area of the rectangle to the console.
To run this program, you can use the following command:
cargo run
This will compile the program and run it. The output of the program should be:
The area of the rectangle is 1500 square pixels.