zoukankan      html  css  js  c++  java
  • Rustlings_structs

    练习过程中的随手记,很多问题也未深究,,简单问题可能直接贴改完之后的代码,..分享出来,若能帮助大家,意外乐趣

    structs1.rs

    // structs1.rs
    // Address all the TODOs to make the tests pass!
    
    struct ColorClassicStruct {
        // TODO: Something goes here
        name:String,
        hex:String
    }
    
    struct ColorTupleStruct(String,String/* TODO: Something goes here */);
    
    #[derive(Debug)]
    struct UnitStruct;
    
    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        fn classic_c_structs() {
            // TODO: Instantiate a classic c struct!
            let green = ColorClassicStruct{
                name : String::from("green"),
                hex : String::from("#00FF00"),
            };
            assert_eq!(green.name, "green");
            assert_eq!(green.hex, "#00FF00");
        }
    
        #[test]
        fn tuple_structs() {
            // TODO: Instantiate a tuple struct!
            let green = ColorTupleStruct(String::from("green"),String::from("#00FF00"));
    
            assert_eq!(green.0, "green");
            assert_eq!(green.1, "#00FF00");
        }
    
        #[test]
        fn unit_structs() {
            // TODO: Instantiate a unit struct!
            let unit_struct = UnitStruct;
            let message = format!("{:?}s are fun!", unit_struct);
    
            assert_eq!(message, "UnitStructs are fun!");
        }
    }
    

    structs2.rs

    // structs2.rs
    // Address all the TODOs to make the tests pass!
    // No hints, just do it!
    
    
    #[derive(Debug)]
    struct Order {
        name: String,
        year: u32,
        made_by_phone: bool,
        made_by_mobile: bool,
        made_by_email: bool,
        item_number: u32,
        count: u32,
    }
    
    fn create_order_template() -> Order {
        Order {
            name: String::from("Bob"),
            year: 2019,
            made_by_phone: false,
            made_by_mobile: false,
            made_by_email: true,
            item_number: 123,
            count: 0,
        }
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        fn your_order() {
            let order_template = create_order_template();
            // TODO: Create your own order using the update syntax and template above!
            let your_order = Order{
                name: String::from("Hacker in Rust"),
                count: 1,
                ..order_template
            };
            assert_eq!(your_order.name, "Hacker in Rust");
            assert_eq!(your_order.year, order_template.year);
            assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
            assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
            assert_eq!(your_order.made_by_email, order_template.made_by_email);
            assert_eq!(your_order.item_number, order_template.item_number);
            assert_eq!(your_order.count, 1);
        }
    }
    
  • 相关阅读:
    SpringBoot发送邮箱验证码
    判断一个数是否为2的整数次幂
    [模板] 虚树 && bzoj2286-[Sdoi2011]消耗战
    [模板] K-D Tree
    [模板] 平衡树: Splay, 非旋Treap, 替罪羊树
    对于约数个数上界的估计
    luogu3702-[SDOI2017]序列计数
    [模板] 线性基
    [模板] 区间mex && 区间元素种数
    bzoj4367-[IOI2014]holiday假期
  • 原文地址:https://www.cnblogs.com/nightwindnw/p/Rustlings_structs.html
Copyright © 2011-2022 走看看