← Каталог
Объектно-ориентированные концепции в Rust — Моделирование поведения через трейты
Фрагмент из «Объектно-ориентированные концепции в Rust»: Моделирование поведения через трейты.
pub trait Shape {
fn area(&self) -> f64;
}
pub struct Circle {
radius: f64,
}
pub struct Rectangle {
width: f64,
height: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
impl Shape for Rectangle {
fn area(&self) -> f64 {
self.width * self.height
}
} pub trait Shape {
fn area(&self) -> f64;
}
pub struct Circle {
radius: f64,
}
pub struct Rectangle {
width: f64,
height: f64,
}
impl Shape for Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
}
impl Shape for Rectangle {
fn area(&self) -> f64 {
self.width * self.height
}
}