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) }}pub struct Canvas { pub shapes: Vec<Box<dyn Renderable>>}impl Canvas { pub fn new() -> Self { Self{shapes: vec![] } } pub fn add_shape(&mut self, b: Box<dyn Renderable>) { self.shapes.push(b); } 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,}pub 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 rendered_shapes = vec!{}; for s in &self.shapes { rendered_shapes.push(s.render()) } rendered_shapes }}// 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// 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>>}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> { let mut all_text: Vec<String> = vec![]; for shape in &self.shapes { all_text.push(shape.render().to_string()); } all_text }}// 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>>}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(|shape| shape.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,}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, x: Box<dyn Renderable>) -> () { self.shapes.push(x); } pub fn render_all(&self) -> Vec<String> { self.shapes .iter() .map(|v| v.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(|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 { // this will result in borrow, hence Canvas can be reuse without change // increase memory overhead allocation 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 { // as trait is based on &self (borrow), any fn that is transformative will result in new collection rather than modification of shape 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> { /* when the trait fn render is invoked, avoid implicit conversion call out to_string() will reduce heap peak to 172.01KB from 190+ however it's redundant and should also be avoided in practice below takes about 4ms however iterator more idiomatic, for smaller size MAY overkill but stick to this version in practice. Pre-allocation memory more efficient. */ self.shapes.iter().map(|s|s.render().to_string()).collect() /* the below reduce heap to 160.6Kb but explicit loop requires re-allocation // however it will increase compile time to 6ms // avoid in practice let mut rendered:Vec<String> = vec![]; for shape in self.shapes { 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,}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{ 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(|shp| shp.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 { 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 out: Vec<String> = Vec::new(); for obj in &self.shapes { out.push(obj.render()) } out }}// 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) }}type Shape = Box<dyn Renderable>;pub struct Canvas { pub shapes : Vec<Shape>}impl Canvas { pub fn new() -> Self { Canvas { shapes: Vec::new() } } pub fn add_shape(&mut self, shape: Shape) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|s|s.render()).collect::<Vec<String>>() }}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,}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 container: Vec<Box<dyn Renderable>>,}impl Canvas { pub fn new() -> Canvas { Canvas { container: Vec::new(), } } pub fn add_shape(&mut self, value_box: Box<dyn Renderable>) { self.container.push(value_box); } pub fn render_all(&self) -> Vec<String> { self.container.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 { 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 { 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().to_string()).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(|shape| shape.render()).collect() // let mut result = Vec::<String>::new(); // for s in &self.shapes { // result.push(s.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 { 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![]} } pub fn add_shape(&mut self, shape: Box<dyn Renderable>) { self.shapes.push(shape); } pub fn render_all(&self) -> Vec<String> { let mut result = Vec::<String>::new(); for s in &self.shapes { result.push(s.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 { 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 { 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() }}// 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 { let val = &self.radius.to_string(); format!("Circle with radius {val}") }}impl Renderable for Rectangle { fn render(&self) -> String { let h = &self.height.to_string(); let w = &self.width.to_string(); format!("Rectangle with width {w} and height {h}") }}// 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 = 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 {} 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, input: Box<dyn Renderable>) { self.shapes.push(input); } pub fn render_all(&self) -> Vec<String> { self.shapes.iter().map(|item| item.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 { 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 ) }}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(|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(|item| item.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 { 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, renderable: Box<dyn Renderable>) { self.shapes.push(renderable) } pub fn render_all(&self) -> Vec<String> { let mut result = Vec::<String>::new(); for shape in &self.shapes { result.push(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 { 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,}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().to_string()).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,}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() }}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,}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>>,}// 1. Implement the trait for Circle and Rectangle// 2. Create the Canvas struct// 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(|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 { 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,}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 { 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 struct#[derive(Default)]pub struct Canvas { shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas { pub fn new() -> Self { Self::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(|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> { let mut res = vec![]; for s in &self.shapes { 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 { 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![]; for s in &self.shapes { 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) }}// 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 { 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(|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 { 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![] } } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s); } pub fn render_all(&self) -> Vec<String> { let mut v:Vec<String> = vec![]; for i in &self.shapes { v.push(i.render().to_string()); } v }}// 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![] } } pub fn add_shape(&mut self, s: Box<dyn Renderable>) { self.shapes.push(s); } pub fn render_all(&self) -> Vec<String> { let mut v:Vec<String> = vec![]; for i in self.shapes.iter() { v.push(i.render()); } v }}// 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 { 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() } }// 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 { 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() }}// 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,}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(|s: &Box<dyn Renderable>| s.render().to_string()) .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, 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,}// 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{ shapes: Vec<Box<dyn Renderable>>,}// 3. Implement the Canvas structimpl Canvas{ pub fn new() -> Self{ return Self { 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 r_v: Vec<String> = vec![]; for i in &self.shapes{ r_v.push(i.render().to_string()); } return r_v; }}// 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(|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> { 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,}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 {0} and height {1}", 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> { return self.shapes.iter().map(|x| x.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 { 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: &Box<dyn Renderable>| x.render().to_string()).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: &Box<dyn Renderable>| 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> { Vec::from_iter(self.shapes.iter().map(|shape| shape.render())) }}// 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 { let radius = self.radius; format!("Circle with radius {radius}") }}impl Renderable for Rectangle { fn render(&self) -> String { let height = self.height; let width = self.width; format!("Rectangle with width {width} and height {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(|r| r.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); }}