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.
		
		
		
		
		
			
		
			
				
					31 lines
				
				875 B
			
		
		
			
		
	
	
					31 lines
				
				875 B
			| 
								 
											4 years ago
										 
									 | 
							
								// errors3.rs
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								// 这是一个试图使用前面练习中 `total_cost` 函数完整版的程序。
							 | 
						||
| 
								 | 
							
								// 但出了些问题!为什么不行?我们需要怎样做才能解决问题?
							 | 
						||
| 
								 | 
							
								// 执行 `rustlings hint errors3` 获取提示!
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								// I AM NOT DONE
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use std::num::ParseIntError;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								fn main() {
							 | 
						||
| 
								 | 
							
								    let mut tokens = 100;
							 | 
						||
| 
								 | 
							
								    let pretend_user_input = "8";
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    let cost = total_cost(pretend_user_input)?;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if cost > tokens {
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        println!("You can't afford that many!");// 译:你的代币不足以完成支付!
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    } else {
							 | 
						||
| 
								 | 
							
								        tokens -= cost;
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        println!("You now have {} tokens.", tokens);// 译:现在你有 {} 个代币"
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
							 | 
						||
| 
								 | 
							
								    let processing_fee = 1;
							 | 
						||
| 
								 | 
							
								    let cost_per_item = 5;
							 | 
						||
| 
								 | 
							
								    let qty = item_quantity.parse::<i32>()?;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    Ok(qty * cost_per_item + processing_fee)
							 | 
						||
| 
								 | 
							
								}
							 |