Now that we know a little bit about trait objects and how to work with them, let's use them in a different example, this challenge focuses on using Box<dyn Trait>
as fields in structs to encapsulate objects with dynamic behavior.
You need to define a trait called Renderable
and create two structs, Circle
and Rectangle
, that implement this trait. Then, you will create another struct called Canvas
, which can hold a collection of objects implementing the Renderable
trait. The Canvas
struct should have methods to add objects and render all objects.
Here are the requirements for the program, make sure to read them carefully:
Renderable
Traitrender(&self) -> String
to represent the object visually.Circle
and Rectangle
StructsCircle
should have a radius: f64
field.Rectangle
should have width: f64
and height: f64
fields.Renderable
TraitCircle
, the render
method should return a string like "Circle with radius X"
.Rectangle
, the render
method should return a string like "Rectangle with width X and height Y"
.Canvas
Structshapes
field that can store a Vec
of the Renderable trait objects.Canvas
:
new() -> Canvas
: Initializes an empty canvas.add_shape()
: Adds a shape to the canvas.render_all()
: Returns a vector of strings, each representing a rendered shape.⚠️ Make sure you make all the relevant items public with the pub
keyword, this is required for the tests to work.
If you're stuck, here are some hints to help you solve the challenge:
Box<dyn Trait>
to store objects with dynamic behavior. e.g.
pub struct Canvas {
pub shapes: Vec<Box<dyn Renderable>>,
}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: vec![], } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle{ fn render(&self) -> String{ format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle{ fn render(&self) -> String{ format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas{ shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas{ pub fn new()-> Canvas { Canvas{ shapes: Vec::<Box<dyn Renderable>>::new(), } } pub fn add_shape(&mut self, shape: Box<dyn Renderable> ){ self.shapes.push(shape); } pub fn render_all(&self)-> Vec<String>{ let mut rendered = Vec::<String>::new(); for shape in self.shapes.iter(){ rendered.push((*shape).render().to_string()); } rendered }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Self { Self { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect() }}// 3. Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, r: Box<dyn Renderable>) { self.shapes.push(r) } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}",self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}",self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Canvas { Canvas { shapes : Vec::new() } } pub fn add_shape(&mut self, shape : Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { let mut renders : Vec<String> = Vec::new(); for renderable_shape in self.shapes.iter() { renders.push(renderable_shape.render()); } renders }}// 3. Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self)-> String { String::from(format!("Circle with radius {}",self.radius)) }}impl Renderable for Rectangle{ fn render(&self)-> String{ String::from(format!("Rectangle with width {} and height {}", self.width, self.height)) }}// 2. Create the Canvas structpub struct Canvas{ shapes : Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas{ pub fn new()->Canvas{ Canvas{ shapes:Vec::new() } } pub fn add_shape(&mut self, new_shape: Box<dyn Renderable>){ self.shapes.push(new_shape) } pub fn render_all(&self)-> Vec<String>{ self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Self { Canvas { shapes: vec!() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s| s.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape) } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas {shapes: Vec::new()} } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s); } pub fn render_all(&self) -> Vec<String> { let mut buffer = vec![]; for s in self.shapes.iter() { buffer.push(s.render()); } return buffer; }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Canvas { Canvas{ shapes: vec![] } } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s| s.render()).collect() }}// 3. Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes:vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s| s.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape) } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 1. Implement the trait for Circle and Rectangle// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { shapes: Vec::new()} } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s) } pub fn render_all(&self) -> Vec<String> { let mut rendered = vec![]; for shape in &self.shapes { rendered.push(shape.render()) } rendered }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {0} and height {1}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new(), } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}#[derive(Default)]pub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Self { Canvas::default() } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Self { Self { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape) } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|sh| sh.render()).collect() }}// 2. Create the Canvas struct// 3. Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { let mut rendered = Vec::<String>::new(); for shape in &self.shapes { rendered.push(shape.render()); } rendered }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { let mut rendered_shapes = Vec::new(); for shape in &self.shapes { rendered_shapes.push(shape.render()); } rendered_shapes }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle{ fn render(&self) -> String{ format!("Circle with radius {}",self.radius) }}impl Renderable for Rectangle{ fn render(&self) -> String{ format!("Rectangle with width {} and height {}",self.width,self.height) }}// 2. Create the Canvas structpub struct Canvas{ shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas{ pub fn new()->Canvas{ Canvas{shapes: vec![]} } pub fn add_shape(&mut self, shape:Box<dyn Renderable>){ self.shapes.push(shape); } pub fn render_all(&self)-> Vec<String>{ self.shapes.iter().map(|x| x.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle{ fn render(&self) -> String{ format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle{ fn render(&self) -> String{ format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas{ shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas{ pub fn new()-> Canvas { Canvas{ shapes: Vec::<Box<dyn Renderable>>::new(), } } pub fn add_shape(&mut self, shape: Box<dyn Renderable> ){ self.shapes.push(shape); } pub fn render_all(&self)-> Vec<String>{ let mut rendered = Vec::<String>::new(); for shape in self.shapes.iter(){ rendered.push((*shape).render().to_string()); } rendered }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { shapes :Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Self { shapes: Vec::new() } } pub fn add_shape(&mut self, p0: Box<dyn Renderable>) { self.shapes.push(p0); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, new_shape :Box<dyn Renderable>) { self.shapes.push(new_shape); } pub fn render_all(&self) -> Vec<String> { let mut shapes_strings = vec![]; for shape in &self.shapes { shapes_strings.push(shape.render()) } shapes_strings }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s| s.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|o| o.render()).collect::<Vec<String>>() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes .iter() .map(|shape| shape.render()) .collect::<Vec<String>>() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { return format!("Circle with radius {}", self.radius); }}impl Renderable for Rectangle { fn render(&self) -> String { return format!("Rectangle with width {} and height {}", self.width, self.height); }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { return self.shapes.iter().map(|shape| shape.render()).collect(); }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new(), } } pub fn add_shape(&mut self, renderable: Box<dyn Renderable>) { self.shapes.push(renderable); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagefn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Self { Self {shapes: Vec::new()} } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect::<Vec<String>>() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::<Box<dyn Renderable>>::new(), } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { let mut renders = Vec::<String>::new(); for shape in self.shapes.iter() { renders.push((*shape).render().to_string()); } renders }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Rectangle { fn render(&self) -> String { format!( "Rectangle with width {} and height {}", self.width, self.height ) }}// 1. Implement the trait for Circle and Rectangle// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes .iter() .map(|shape| shape.render()) .collect::<Vec<_>>() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Self { Self { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) -> () { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect::<Vec<String>>() }}// Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }} impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { shapes: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas{ shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s| s.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s) } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|x| x.render()).collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 2. Create the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shapes: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes .iter() .map(|s| s.render()) .collect() }}// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}
pub trait Renderable { fn render(&self) -> String;}pub struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}impl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius) }}impl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { // Initializes an empty canvas. pub fn new() -> Self { Canvas { shapes: Vec::new(), } } // Adds a shape to the canvas. pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } // Returns a vector of strings, each representing a rendered shape. pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|shape| shape.render()).collect() }}// 1. Implement the trait for Circle and Rectangle// 2. Create the Canvas struct// 3. Implement the Canvas struct// Example usagepub fn main() { let mut canvas = Canvas::new(); canvas.add_shape(Box::new(Circle { radius: 5.0 })); canvas.add_shape(Box::new(Rectangle { width: 3.0, height: 4.0, })); let rendered_shapes = canvas.render_all(); for shape in rendered_shapes { println!("{}", shape); }}