50.13.1Shapes drawing¶
We define first the Shape trait:
trait Shape {
fn draw(&self);
}Then the rectangle:
struct Rectangle {
x: i32,
y: i32,
width: u32,
height: u32,
}Its implementation:
impl Shape for Rectangle {
fn draw(&self) {
println!("Drawing rectangle {}x{} at ({}, {}).",
self.width, self.height, self.x, self.y);
}
}The Circle structure:
struct Circle {
x: i32,
y: i32,
radius: u32,
}Its implementation:
impl Shape for Circle {
fn draw(&self) {
println!("Drawing circle of radius {} at ({}, {}).",
self.radius, self.x, self.y);
}
}The Drawing contains now a shapes field that is a vector of
shapes:
struct Drawing<'a> {
shapes: Vec<&'a (dyn Shape + 'a)>,
}In the implementation of the Drawing structure, we now have a
draw() method that iterates over all shapes to call their draw()
methods, and a add_shape() method to add new shapes:
impl<'a> Drawing<'a> {
fn draw(&self) {
for shape in &self.shapes {
shape.draw();
}
}
fn add_shape(&mut self, shape: &'a dyn Shape) {
self.shapes.push(shape);
}
}Let us instantiate a rectangle and a circle:
let rect = Rectangle { x: 4, y: 6, width: 20, height: 30 };
let circ = Circle { x: 5, y: 2, radius: 10 };We then create the Drawing object and add our shapes to it, and
finally trigger the drawing:
{
let mut drawing = Drawing { shapes: Vec::new() };
drawing.add_shape(&rect);
drawing.add_shape(&circ);
drawing.draw();
}Drawing rectangle 20x30 at (4, 6).
Drawing circle of radius 10 at (5, 2).
()