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 objects: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { objects: Vec::new() } } pub fn add_shape(&mut self, object: Box<dyn Renderable>) { self.objects.push(object); } pub fn render_all(&self) -> Vec<String> { self.objects.iter().map(|o| o.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) }}pub struct Canvas { 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(|shape| shape.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() -> Canvas{ return 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 res:Vec<String>= vec![]; for s in self.shapes.iter() { // res.push(s.render()); res.push((*s.render()).to_string()); } return res; }}// 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{ return 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 res:Vec<String>= vec![]; for s in self.shapes.iter() { res.push((*s).render()); } res }}// 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{ return 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 res:Vec<String>= vec![]; for s in self.shapes.iter() { res.push((*s).render()); } return res; }}// 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{ return 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 res:Vec<String>= vec![]; for s in self.shapes.iter() { res.push(s.render()); } return res; }}// 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{ return 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 res:Vec<String>= vec![]; for s in self.shapes.iter() { res.push(s.render()); } return res; }}// 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(|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::<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> = Vec::new(); for shape in self.shapes.iter() { 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 { // add code here 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 objs: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Canvas { objs: Vec::new() } } pub fn add_shape(&mut self, obj: Box<dyn Renderable>) { self.objs.push(obj); } pub fn render_all(&self) -> Vec<String> { self.objs.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 shape: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas {shape: vec![]} } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shape.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shape.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;}#[derive(Default)]pub struct Circle { pub radius: f64,}#[derive(Default)]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 struct#[derive(Default)]pub 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 shape_vec: Vec<String> = vec![]; for shape in self.shapes.iter() { shape_vec.push(shape.render()); } shape_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,}// 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::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 struct Circle { pub radius: f64,}pub struct Rectangle { pub width: f64, pub height: f64,}pub trait Renderable { fn render(&self) -> String;}// 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 vec_strings: Vec<String> = vec![]; for string in self.shapes.iter() { vec_strings.push((*string.render()).to_string()); } vec_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) }}// 1. Implement the trait for Circle and Rectangle// 2. Create the Canvas struct// 3. Implement 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> { 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, b: Box<dyn Renderable>) { self.shapes.push(b) } pub fn render_all(&self) -> Vec<String> { let mut rendered: Vec<String> = vec![]; 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 { 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> { 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() -> 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,}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() -> 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(|x| x.render()).collect() }}pub 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>>,}impl Canvas { pub fn new() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, obj: Box<dyn Renderable>) { self.shapes.push(obj); } 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,}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) }}// 2. Create the Canvas structimpl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 3. Implement the Canvas structpub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>,}impl 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> { let mut vecs = Vec::<String>::new(); self.shapes.iter().for_each(|item| { vecs.push(item.render()); }); vecs }}// 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 ) }}// 1. Implement the trait for Circle and Rectanglepub struct Canvas { 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(|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() -> 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(|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() -> Canvas { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, item: Box<dyn Renderable>){ self.shapes.push(item); } pub fn render_all(&self) -> Vec<String>{ self.shapes.iter().map(|i| i.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>>,}impl 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() }}pub 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 shape: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { shape:Vec::new(), } } pub fn add_shape(&mut self, other_shape: Box<dyn Renderable>) { self.shape.push(other_shape); } pub fn render_all(&self)-> Vec<String>{ let mut result = Vec::new(); for temp_shape in self.shape.iter(){ result.push(temp_shape.render()); } result }}// 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 shape: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Canvas { Canvas { shape: vec![] } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shape.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shape.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 shape: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self { shape:Vec::new(), } } pub fn add_shape(&mut self, other_shape: Box<dyn Renderable>) { self.shape.push(other_shape); } pub fn render_all(&self)-> Vec<String>{ let mut result = Vec::new(); for temp_shape in self.shape.iter(){ result.push(temp_shape.render()); } result }}// 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 { shape: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self{ shape:Vec::new(), } } pub fn add_shape(&mut self,other_shape:Box<dyn Renderable>) { self.shape.push(other_shape) } pub fn render_all(self) ->Vec<String> { self.shape.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 { shape: Vec<Box<dyn Renderable>>}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self{ shape:Vec::new(), } } pub fn add_shape(&mut self,other_shape:Box<dyn Renderable>) { self.shape.push(other_shape) } pub fn render_all(self) ->Vec<String> { self.shape.into_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() -> 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(|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() -> 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 = Vec::new(); for shape in &self.shapes { shapes.push(shape.render()); } 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() -> 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 = Vec::new(); for shape in &self.shapes { shapes.push(shape.render()); } 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>>,}impl Canvas { pub fn new() -> Self { Self { 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> = vec![]; for shape in self.shapes.iter() { rendered.push((*shape.render()).to_string()); }// self.shapes.iter().for_each(|e|rendered.push(e.render())); rendered }}// 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 { shapes: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Self { Self { 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> = vec![]; self.shapes.iter().for_each(|e|rendered.push(e.render())); rendered }}// 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() -> Canvas { 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> { 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() -> 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,}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 ) }}// 2. Create the Canvas structpub struct Canvas { pub 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::<Vec<String>>() }}// 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) }}// 2. Create the Canvas structimpl Renderable for Rectangle { fn render(&self) -> String { format!("Rectangle with width {} and height {}", self.width, self.height) }}// 3. Implement 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, val: Box<dyn Renderable>) { self.shapes.push(val); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|val| val.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 ) }}pub struct Canvas { pub 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(|s| s.render()).collect() }}pub 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> { let mut rendered_shapes: Vec<String> = vec![]; for shape in self.shapes.iter() { 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,} 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::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() }}// 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); }}
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::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() }}// 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); }}
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::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 { 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> { let mut string_vec: Vec<String> = Vec::new(); for elm in self.shapes.iter() { string_vec.push(elm.render()) } string_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,}// 1. Implement the trait for Circle and Rectangleimpl Renderable for Circle { fn render(&self) -> String { format!("Circle with radius {}", self.radius.to_string()) }}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::new() } } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape) } pub fn render_all(&self) -> Vec<String> { let mut string_vec: Vec<String> = Vec::new(); for elm in self.shapes.iter() { string_vec.push(elm.render()) } string_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,}// 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> { let mut ans: Vec<String> = vec![]; for shape in self.shapes.iter() { ans.push(shape.render()); } ans }}// 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 res:Vec<String> = vec![]; for shape in self.shapes.iter() { res.push(shape.render()); } res }}// 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(|x| x.render()).collect() }}
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(|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); }}