mirror of https://github.com/sunface/rust-course
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					83 lines
				
				2.5 KiB
			
		
		
			
		
	
	
					83 lines
				
				2.5 KiB
			| 
								 
											4 years ago
										 
									 | 
							
								// structs3.rs
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// 接口既可以包含数据也可以处理逻辑。
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// 在这个练习中,我们已经定义了 Package 结构,但我们想测试一些根据它实现的逻辑。
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// 让代码通过编译和测试!
							 | 
						||
| 
								 | 
							
								// 如果你有问题,可以执行 `rustlings hint structs3` 查看提示
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								// I AM NOT DONE
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#[derive(Debug)]
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								struct Package {// 译:包裹
							 | 
						||
| 
								 | 
							
								    sender_country: String,// 译:寄件人国家
							 | 
						||
| 
								 | 
							
								    recipient_country: String,// 译:收件人国家
							 | 
						||
| 
								 | 
							
								    weight_in_grams: i32,// 译:重量(克)
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								impl Package {
							 | 
						||
| 
								 | 
							
								    fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
							 | 
						||
| 
								 | 
							
								        if weight_in_grams <= 0 {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								            // 这里需要完成一些东西……
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            Package {
							 | 
						||
| 
								 | 
							
								                sender_country,
							 | 
						||
| 
								 | 
							
								                recipient_country,
							 | 
						||
| 
								 | 
							
								                weight_in_grams,
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    fn is_international(&self) -> ??? {// 译:是否是国际上的
							 | 
						||
| 
								 | 
							
								        // 这里需要完成一些东西……
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    fn get_fees(&self, cents_per_gram: i32) -> ??? {// 译:获取所需费用
							 | 
						||
| 
								 | 
							
								        // 这里需要完成一些东西……
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#[cfg(test)]
							 | 
						||
| 
								 | 
							
								mod tests {
							 | 
						||
| 
								 | 
							
								    use super::*;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    #[test]
							 | 
						||
| 
								 | 
							
								    #[should_panic]
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    fn fail_creating_weightless_package() {// 译:失败地创建没有重量的包裹
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        let sender_country = String::from("Spain");// 译:西班牙
							 | 
						||
| 
								 | 
							
								        let recipient_country = String::from("Austria");// 译:奥地利
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								        Package::new(sender_country, recipient_country, -2210);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    #[test]
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    fn create_international_package() {// 译:创建国际上的包裹
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        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]
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    fn calculate_transport_fees() {// 译:计算运输费
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        let sender_country = String::from("Spain");
							 | 
						||
| 
								 | 
							
								        let recipient_country = String::from("Spain");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        let cents_per_gram = 3;// 译:分/克(一克需要多少分钱)
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								        let package = Package::new(sender_country, recipient_country, 1500);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        assert_eq!(package.get_fees(cents_per_gram), 4500);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |