Rust
Beginner
1 min read
Methods and Associated Functions with impl
Example
#[derive(Debug)]
struct Rectangle {
width: f64,
height: f64,
}
impl Rectangle {
// Associated function (constructor)
pub fn new(width: f64, height: f64) -> Self {
assert!(width > 0.0 && height > 0.0, "dimensions must be positive");
Rectangle { width, height }
}
// Immutable method
pub fn area(&self) -> f64 {
self.width * self.height
}
pub fn perimeter(&self) -> f64 {
2.0 * (self.width + self.height)
}
pub fn is_square(&self) -> bool {
(self.width - self.height).abs() < f64::EPSILON
}
// Takes another Rectangle by reference
pub fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
// Mutable method
pub fn scale(&mut self, factor: f64) {
self.width *= factor;
self.height *= factor;
}
}
fn main() {
let mut r1 = Rectangle::new(10.0, 5.0);
let r2 = Rectangle::new(3.0, 2.0);
println!("r1 = {:?}", r1);
println!("area = {} perimeter = {}", r1.area(), r1.perimeter());
println!("is square: {} can hold r2: {}", r1.is_square(), r1.can_hold(&r2));
r1.scale(2.0);
println!("after scale(2): {:?}", r1);
}