#Rust ~ Need help with Enum, method impl

1 messages · Page 1 of 1 (latest)

frank tulip
#
        match *self {
            Shape::Circle { radius } => assert!(radius >= 0.0, "Enter a Positive Number"),
            Shape::Rectangle { width, height } => assert!(width >= 0.0 && height >= 0.0, "Both values must be 
Positive"),
            Shape::Triangle { a, b, c } => 
        }
    } ```
#

this is the method I need help on

#
    Circle { radius: f64 },
    Rectangle { width: f64, height: f64 },
    Triangle { a: f64, b: f64, c: f64 },
}

impl Shape {
    fn new(lengths: &[f64]) -> Option<Shape> {
        match lengths.len() {
            1 => Some(Shape::Circle { radius: lengths[0] }),
            2 => Some(Shape::Rectangle {
                width: lengths[0],
                height: lengths[1],
            }),
            3 => Some(Shape::Triangle {
                a: lengths[0],
                b: lengths[1],
                c: lengths[2],
            }),
            _ => None,
        }
    }

    fn area(&self) -> f64 {
        match *self {
            Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
            Shape::Rectangle { width, height } => width * height,
            Shape::Triangle { a, b, c } => {
                let s = (a + b + c) / 2.0;
                let a = s * (s - a) * (s - b) * (s - c);
                a.sqrt()
            }
        }
    }

    fn perimeter(&self) -> f64 {
        match *self {
            Shape::Circle { radius } => std::f64::consts::PI * radius * 2.0,
            Shape::Rectangle { width, height } => 2.0 * (height * width),
            Shape::Triangle { a, b, c } => a + b + c,
        }
    }

    fn double(&self) -> Shape {
        match *self {
            Shape::Circle { radius } => Shape::Circle {
                radius: std::f64::consts::PI * radius * 2.0,
            },
            Shape::Rectangle { width, height } => Shape::Rectangle {
                width,
                height: 2.0 * (height + width),
            },
            Shape::Triangle { a, b, c } => Shape::Triangle { a, b, c: a + b + c },
        }
    }```