Giter Site home page Giter Site logo

rust_4's Introduction

第四课作业

代码运行

57PEm4.png

第一题

    println!("第一题枚举信号灯");
    println!("------------");
    let red = SignalLight::RED(20);
    let green = SignalLight::GREEN(25);
    let yellow = SignalLight::YELLOW(5);
    println!("红灯:{}秒", red.time());
    println!("绿灯:{}秒", green.time());
    println!("黄灯:{}秒", yellow.time());
    println!("------------");
    enum SignalLight {
        RED(u32),
        GREEN(u32),
        YELLOW(u32),
    }

    trait LightTime {
        fn time(&self) -> u32;
    }

    impl LightTime for SignalLight {
        fn time(&self) -> u32 {
            match self {
                &Self::RED(v) => v,
                &Self::GREEN(v) => v,
                &Self::YELLOW(v) => v,
            }
        }
    }

第二题

    println!("第二题整数求和");
    println!("------------");
    let v = vec![1, 2, 3, 5];
    if let Some(value) = sum(&v) {
        println!("{}", value);
    } else {
        println!("结果溢出了")
    }
    println!("------------");
    fn sum(v: &[u32]) -> Option<u32> {
        let mut result: u32 = 0;
        for &item in v.iter() {
            let value = match result.checked_add(item) {
                None => return None,
                Some(v) => v,
            };
            result = value;
        }
        Some(result)
    }

第三题

    println!("第三题计算面积");
    println!("------------");
    let c = Circle {
        radius: 1.0,
        name: String::from("圆形"),
    };
    let t = Triangle {
        base: 2.0,
        height: 3.0,
        name: String::from("三角形"),
    };
    let s = Square {
        length: 2.0,
        name: String::from("正方形"),
    };
    let calculator = Calculator { area: c };
    calculator.print_area();
    print_and_calculate(&s);
    print_and_calculate(&t);
    println!("------------");

    fn print_and_calculate<T: Area>(area: &T) {
        println!("{} - 面积是:{}", area.name(), area.area())
    }

    struct Calculator<T>
    where
        T: Area,
    {
        area: T,
    }

    impl<T> Calculator<T>
    where
        T: Area,
    {
        fn instance(&self) -> &T {
            &self.area
        }

        fn print_area(&self) {
            println!(
                "{} - 面积是:{}",
                self.instance().name(),
                self.instance().area()
            );
        }
    }

    trait Area {
        fn area(&self) -> f64;
        fn name(&self) -> &str;
    }

    struct Triangle {
        name: String,
        base: f64,
        height: f64,
    }

    struct Circle {
        name: String,
        radius: f64,
    }

    struct Square {
        name: String,
        length: f64,
    }

    impl Area for Triangle {
        fn area(&self) -> f64 {
            (self.base * self.height) / 2.0
        }
        fn name(&self) -> &str {
            &self.name
        }
    }

    impl Area for Circle {
        fn area(&self) -> f64 {
            self.radius * self.radius * 3.14
        }

        fn name(&self) -> &str {
            &self.name
        }
    }

    impl Area for Square {
        fn area(&self) -> f64 {
            self.length * self.length
        }

        fn name(&self) -> &str {
            &self.name
        }
    }

rust_4's People

Contributors

xingfly avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.