Structs in RUST
Structs are used to define custom data types in Rust. They can have fields with different types and methods to operate on those fields. Here's an example:
rust
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect = Rectangle { width: 10, height: 20 };
println!("The area of the rectangle is {} square pixels.", rect.area());
}
No comments:
Post a Comment