Skip to content
On this page

结构体3: exercises/structs/structs3.rs

题目

rust
// structs3.rs
//
// Structs contain data, but can also have logic. In this exercise we have
// defined the Package struct and we want to test some logic attached to it.
// Make the code compile and the tests pass!
//
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

#[derive(Debug)]
struct Package {
    sender_country: String,
    recipient_country: String,
    weight_in_grams: i32,
}

impl Package {
    fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
        if weight_in_grams <= 0 {
            panic!("Can not ship a weightless package.")
        } else {
            Package {
                sender_country,
                recipient_country,
                weight_in_grams,
            }
        }
    }

    fn is_international(&self) -> ??? {
        // Something goes here...
    }

    fn get_fees(&self, cents_per_gram: i32) -> ??? {
        // Something goes here...
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic]
    fn fail_creating_weightless_package() {
        let sender_country = String::from("Spain");
        let recipient_country = String::from("Austria");

        Package::new(sender_country, recipient_country, -2210);
    }

    #[test]
    fn create_international_package() {
        let sender_country = String::from("Spain");
        let recipient_country = String::from("Russia");

        let package = Package::new(sender_country, recipient_country, 1200);

        assert!(package.is_international());
    }

    #[test]
    fn create_local_package() {
        let sender_country = String::from("Canada");
        let recipient_country = sender_country.clone();

        let package = Package::new(sender_country, recipient_country, 1200);

        assert!(!package.is_international());
    }

    #[test]
    fn calculate_transport_fees() {
        let sender_country = String::from("Spain");
        let recipient_country = String::from("Spain");

        let cents_per_gram = 3;

        let package = Package::new(sender_country, recipient_country, 1500);

        assert_eq!(package.get_fees(cents_per_gram), 4500);
        assert_eq!(package.get_fees(cents_per_gram * 2), 9000);
    }
}

结构体包含数据,也可以包含逻辑,在这个练习中,我们定义了Package结构体,并且想要测试它的一些逻辑。

我们需要修改代码,使单元测试顺利通过。

题目解析

结构体Package包含三个字段:

  • sender_country: 发件人国家
  • recipient_country: 收件人国家
  • weight_in_grams: 包裹重量

然后定义三个方法:

  • new:新建包裹,要求包裹重量必须>=0。
  • is_international: 是否是国际包裹
  • get_fees: 获取费用信息

我们再来看单元测试:

  • fail_creating_weightless_package用于测试new方法,传递的包裹重量为负数,所以会panic
  • create_international_packagecreate_local_package用于测试is_international方法,当国家不一样的时候,就应该是国际包裹。
  • calculate_transport_fees用于测试get_fees方法:
    • 重量为3,当单价为1500时需要花费4500,当单价为3000时需要花费9000

捋清楚了逻辑,我们就可以补充代码了:

rust
impl Package {
    fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
        if weight_in_grams <= 0 {
            panic!("Can not ship a weightless package.")
        } else {
            Package {
                sender_country,
                recipient_country,
                weight_in_grams,
            }
        }
    }

    fn is_international(&self) -> ??? { 
    fn is_international(&self) -> bool { 
        // Something goes here...
        if self.sender_country != self.recipient_country { 
            true 
        } else { 
            false 
        } 
    }

    fn get_fees(&self, cents_per_gram: i32) -> ??? { 
    fn get_fees(&self, cents_per_gram: i32) -> i32 { 
        // Something goes here...
        cents_per_gram * self.weight_in_grams 
    }
}

参考资料

Powered by VitePress