|
|
|
@ -1,22 +1,22 @@
|
|
|
|
|
// 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!
|
|
|
|
|
// If you have issues execute `rustlings hint structs3`
|
|
|
|
|
// 接口既可以包含数据也可以处理逻辑。
|
|
|
|
|
// 在这个练习中,我们已经定义了 Package 结构,但我们想测试一些根据它实现的逻辑。
|
|
|
|
|
// 让代码通过编译和测试!
|
|
|
|
|
// 如果你有问题,可以执行 `rustlings hint structs3` 查看提示
|
|
|
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Package {
|
|
|
|
|
sender_country: String,
|
|
|
|
|
recipient_country: String,
|
|
|
|
|
weight_in_grams: i32,
|
|
|
|
|
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 {
|
|
|
|
|
// Something goes here...
|
|
|
|
|
// 这里需要完成一些东西……
|
|
|
|
|
} else {
|
|
|
|
|
Package {
|
|
|
|
|
sender_country,
|
|
|
|
@ -26,12 +26,12 @@ impl Package {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_international(&self) -> ??? {
|
|
|
|
|
// Something goes here...
|
|
|
|
|
fn is_international(&self) -> ??? {// 译:是否是国际上的
|
|
|
|
|
// 这里需要完成一些东西……
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_fees(&self, cents_per_gram: i32) -> ??? {
|
|
|
|
|
// Something goes here...
|
|
|
|
|
fn get_fees(&self, cents_per_gram: i32) -> ??? {// 译:获取所需费用
|
|
|
|
|
// 这里需要完成一些东西……
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -41,15 +41,15 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_panic]
|
|
|
|
|
fn fail_creating_weightless_package() {
|
|
|
|
|
let sender_country = String::from("Spain");
|
|
|
|
|
let recipient_country = String::from("Austria");
|
|
|
|
|
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() {
|
|
|
|
|
fn create_international_package() {// 译:创建国际上的包裹
|
|
|
|
|
let sender_country = String::from("Spain");
|
|
|
|
|
let recipient_country = String::from("Russia");
|
|
|
|
|
|
|
|
|
@ -69,11 +69,11 @@ mod tests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn calculate_transport_fees() {
|
|
|
|
|
fn calculate_transport_fees() {// 译:计算运输费
|
|
|
|
|
let sender_country = String::from("Spain");
|
|
|
|
|
let recipient_country = String::from("Spain");
|
|
|
|
|
|
|
|
|
|
let cents_per_gram = 3;
|
|
|
|
|
let cents_per_gram = 3;// 译:分/克(一克需要多少分钱)
|
|
|
|
|
|
|
|
|
|
let package = Package::new(sender_country, recipient_country, 1500);
|
|
|
|
|
|
|
|
|
|