Sha256: 40934b4934e54bbc72b571643e05a9b8bb13e21afa756a78c0ea4d11c97ec7cd

Contents?: true

Size: 1.23 KB

Versions: 396

Compression:

Stored size: 1.23 KB

Contents

extern crate num;

use num::Num;

pub struct Triangle<T> {
    sides: [T; 3],
}

impl<T: Num + PartialOrd + PartialEq + Copy> Triangle<T> {
    #[cfg_attr(rustfmt, rustfmt_skip)]
    fn valid_sides(&self) -> bool {
        let z = self.sides[0] - self.sides[0];
        (self.sides.iter().all(|&s| s > z)) &&
            (self.sides[0] + self.sides[1] >= self.sides[2]) &&
            (self.sides[1] + self.sides[2] >= self.sides[0]) &&
            (self.sides[2] + self.sides[0] >= self.sides[1])
    }

    pub fn build(sides: [T; 3]) -> Result<Self, ()> {
        let t = Triangle { sides: sides };

        if t.valid_sides() {
            Ok(t)
        } else {
            Err(())
        }
    }

    pub fn is_equilateral(&self) -> bool {
        let a = self.sides[0];
        let b = self.sides[1];
        let c = self.sides[2];

        a == b && b == c
    }

    pub fn is_isosceles(&self) -> bool {
        let a = self.sides[0];
        let b = self.sides[1];
        let c = self.sides[2];

        (a == b && a != c) || (a == c && a != b) || (b == c && a != b)
    }

    pub fn is_scalene(&self) -> bool {
        let a = self.sides[0];
        let b = self.sides[1];
        let c = self.sides[2];

        a != b && a != c && b != c
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.179 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.178 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.177 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.176 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.175 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.174 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.173 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.172 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.171 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.170 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.169 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.167 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.166 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.165 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.164 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.163 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.162 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.161 tracks/rust/exercises/triangle/float_example.rs
trackler-2.2.1.160 tracks/rust/exercises/triangle/float_example.rs