如何在Rust中组合if let和match表达式

huangapple go评论97阅读模式
英文:

How to combine if let match expression in rust

问题

Here is the translated code portion:

  1. fn main() {
  2. println!("\n欢迎。这是温度转换器");
  3. println!("我可以处理华氏度、摄氏度和开尔文度");
  4. println!("\n请输入要转换的值:");
  5. loop {
  6. let mut value = String::new();
  7. std::io::stdin()
  8. .read_line(&mut value)
  9. .expect("读取输入失败");
  10. let value: f32 = match value.trim().parse() {
  11. Ok(num) => num,
  12. Err(_) => {
  13. println!("请输入数值。");
  14. continue;
  15. }
  16. };
  17. println!("谢谢,我已经获得了值:{}。", value);
  18. println!("请提供其相应的度量单位。");
  19. println!("K:开尔文 C:摄氏度 F:华氏度:");
  20. let mut unit = String::new();
  21. std::io::stdin()
  22. .read_line(&mut unit)
  23. .expect("读取输入失败");
  24. let unit = unit.trim().to_uppercase();
  25. let unit = match unit.as_str() {
  26. "K" => "K",
  27. "F" => "F",
  28. "C" => "C",
  29. _ => {
  30. println!("请输入K、F或C,分别表示开尔文、华氏度或摄氏度。");
  31. continue;
  32. }
  33. };
  34. println!("谢谢您的输入。我看到您要将 {} 度 {} 进行转换。", value, unit);
  35. println!("请稍候,我将提供所有可能的转换。\n这将很快完成。");
  36. if unit == "K" {
  37. let value_c = value - 273.15;
  38. let value_f = (((value - 273.15) * 9.0) + (32.0 * 5.0)) / 5.0;
  39. println!("{}{} 转换为:{}C 和 {}F", value, unit, value_c, value_f);
  40. println!("感谢您使用温度转换器。很高兴为您提供帮助。再见!");
  41. break;
  42. } else if unit == "C" {
  43. let value_k = value + 273.15;
  44. let value_f = (value * (9.0 / 5.0)) + 32.0;
  45. println!("{}{} 转换为:{}K 和 {}F", value, unit, value_k, value_f);
  46. println!("感谢您使用温度转换器。很高兴为您提供帮助。再见!");
  47. break;
  48. } else if unit == "F" {
  49. let value_k = (((value - 32.0) * 5.0) / 9.0) + 273.15;
  50. let value_c = ((value - 32.0) * 5.0) / 9.0;
  51. println!("{}{} 转换为:{}K 和 {}C", value, unit, value_k, value_c);
  52. println!("感谢您使用温度转换器。很高兴为您提供帮助。再见!");
  53. break;
  54. }
  55. }
  56. }

Please note that I've replaced the HTML entities (") with regular double quotes (") for readability.

英文:

//here is my code. It is a Temperature Converter that takes input for Kelvin, Celsius or Fahrenheit and outputs converted value for corresponding contrary units.

// Problem:

// it will ask value first, which must be number, so the loop restarts. this is OK
// it will then ask for unit, which must be k,c or f. Upon different entry, it does catches and print "input k for kelvin, c for celcius F for Fahrenheit"
// But input doesnot register and jumps to the begening of the loop asking for value in numbers.

//All in all i want to catch the unit variable with k,c or f else ask the user to do so, not jump to the begening of loop.

// i am learning rust so excuse me for anything out of the norm.

//Here is my main.rs:

  1. fn main() {
  2. println!("\nWelcome. This is Temperature-Converter\n");
  3. println!("I work with Fahrenheit, Celsius and Kelvin");
  4. println!("\nPlease Input the Value you want to convert: ");
  5. loop{
  6. let mut value = String::new();
  7. std::io::stdin().read_line(&mut value).expect("Failed to read line");
  8. let value:f32 = match value.trim().parse(){
  9. Ok(num) => num,
  10. Err(_) => {
  11. println!("please input value in number(s).");
  12. continue;
  13. }
  14. };
  15. println!("Thank You, I have the value: {value}.\nPlease provide it's respective unit of measurement.");
  16. println!("K : for Kelvin C : for Celsius F : for Fahrenheit : ");
  17. let mut unit = String::new();
  18. std::io::stdin().read_line(&mut unit).expect("Failed to read line");
  19. let unit = unit.trim().to_uppercase();
  20. let unit = match unit.as_str() {
  21. "K"=>"K",
  22. "F"=>"F",
  23. "C"=>"C",
  24. _=>{
  25. println!("please input K, F or C for Kelvin, Fahrenheit or Celcius respectively.");
  26. continue;
  27. }
  28. };
  29. println!("Thank you for your input. I see that you want to convert {value} degree {unit}.");
  30. println!("Please wait while i provide all possible conversions.\nThis will be quick.");
  31. if unit == "K"{
  32. let value_c = value-273.15;
  33. let value_f = (((value-273.15)*9.0)+(32.0*5.0))/5.0;
  34. println!("{value}{unit} is converted to: {value_c}C and {value_f}F");
  35. println!("Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!");
  36. break;
  37. }
  38. else if unit == "C"{
  39. let value_k = value+273.15;
  40. let value_f = (value*(9.0/5.0))+32.0;
  41. println!("{value}{unit} is converted to:{value_k}K and {value_f}F");
  42. println!("Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!");
  43. break;
  44. }
  45. else if unit == "F"{
  46. let value_k = (((value-32.0)*5.0)/9.0)+273.15;
  47. let value_c = ((value-32.0)*5.0)/9.0;
  48. println!("{value}{unit} is Converted to: {value_k}K and {value_c}C");
  49. println!("Thank You for using Temperature-Converter. It was a pleasure to assist you. Byee!!");
  50. break;
  51. };
  52. }
  53. }

//i tried :

  1. if let unit = match unit.as_str() {
  2. "K"=>"K",
  3. "F"=>"F",
  4. "C"=>"C",
  5. _=>{println!("please input K, F or C for Kelvin, Fahrenheit or Celcius respectively.")}
  6. };

// In fact i am having hard time understanding "match" and scope and return value of "loop"

答案1

得分: 0

  1. 程序逻辑仍可简化,目前只处理了语法问题:
  2. fn main() {
  3. println!("\n欢迎。这是温度转换器\n");
  4. println!("我可以处理华氏度、摄氏度和开尔文");
  5. println!("\n请输入您想转换的值:");
  6. loop {
  7. let mut input = String::new();
  8. let mut value = 0.0_f32;
  9. match std::io::stdin().read_line(&mut input) {
  10. Ok(_) => {
  11. value = match input.trim().parse() {
  12. Ok(num) => num,
  13. Err(_) => {
  14. println!("请以数字形式输入值。");
  15. continue;
  16. }
  17. };
  18. println!("谢谢,我已经得到值:{value}。");
  19. }
  20. Err(_) => println!("读取行失败"),
  21. }
  22. println!("请提供相应的计量单位。");
  23. println!("K:表示开尔文 C:表示摄氏度 F:表示华氏度:");
  24. let mut unit;
  25. loop {
  26. unit = String::new();
  27. match std::io::stdin().read_line(&mut unit) {
  28. Ok(_) => {
  29. unit = unit.trim().to_uppercase();
  30. match unit.as_str() {
  31. "K" | "F" | "C" => break,
  32. _ => println!("请分别输入 K、F 或 C,表示开尔文、华氏度或摄氏度。"),
  33. }
  34. }
  35. Err(_) => println!("读取行失败"),
  36. }
  37. }
  38. println!("感谢您的输入。我看到您想将 {value} 度 {unit} 进行转换。");
  39. println!("请稍候,我将提供所有可能的转换。\n这将很快完成。");
  40. if unit == "K" {
  41. let value_c = value - 273.15;
  42. let value_f = (((value - 273.15) * 9.0) + (32.0 * 5.0)) / 5.0;
  43. println!("{value}{unit} 转换为:{value_c} 摄氏度和 {value_f} 华氏度");
  44. println!("感谢使用温度转换器。很高兴能帮助您。再见!");
  45. break;
  46. } else if unit == "C" {
  47. let value_k = value + 273.15;
  48. let value_f = (value * (9.0 / 5.0)) + 32.0;
  49. println!("{value}{unit} 转换为:{value_k} 开尔文和 {value_f} 华氏度");
  50. println!("感谢使用温度转换器。很高兴能帮助您。再见!");
  51. break;
  52. } else if unit == "F" {
  53. let value_k = (((value - 32.0) * 5.0) / 9.0) + 273.15;
  54. let value_c = ((value - 32.0) * 5.0) / 9.0;
  55. println!("{value}{unit} 转换为:{value_k} 开尔文和 {value_c} 摄氏度");
  56. println!("感谢使用温度转换器。很高兴能帮助您。再见!");
  57. break;
  58. };
  59. }
  60. }
英文:

The program logic can still be simplified, for now only the syntactical problems are dealt with here:

  1. fn main() {
  2. println!("\nWelcome. This is Temperature-Converter\n");
  3. println!("I work with Fahrenheit, Celsius and Kelvin");
  4. println!("\nPlease Input the Value you want to convert: ");
  5. loop {
  6. let mut input = String::new();
  7. let mut value = 0.0_f32;
  8. match std::io::stdin().read_line(&mut input) {
  9. Ok(_) => {
  10. value = match input.trim().parse() {
  11. Ok(num) => num,
  12. Err(_) => {
  13. println!("please input value in number(s).");
  14. continue;
  15. }
  16. };
  17. println!("Thank You, I have the value: {value}.");
  18. }
  19. Err(_) => println!("Failed to read line"),
  20. }
  21. println!("Please provide it's respective unit of measurement.");
  22. println!("K: for Kelvin C: for Celsius F: for Fahrenheit : ");
  23. let mut unit;
  24. loop {
  25. unit = String::new();
  26. match std::io::stdin().read_line(&mut unit) {
  27. Ok(_) => {
  28. unit = unit.trim().to_uppercase();
  29. match unit.as_str() {
  30. "K" | "F" | "C" => break,
  31. _ => println!("please input K, F or C for Kelvin, Fahrenheit or Celcius respectively."),
  32. }
  33. }
  34. Err(_) => println!("Failed to read line"),
  35. }
  36. }
  37. println!("Thank you for your input. I see that you want to convert {value} degree {unit}.");
  38. println!("Please wait while i provide all possible conversions.\nThis will be quick.");
  39. if unit == "K" {
  40. let value_c = value - 273.15;
  41. let value_f = (((value - 273.15) * 9.0) + (32.0 * 5.0)) / 5.0;
  42. println!("{value}{unit} is converted to: {value_c}C and {value_f}F");
  43. println!("Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!");
  44. break;
  45. } else if unit == "C" {
  46. let value_k = value + 273.15;
  47. let value_f = (value * (9.0 / 5.0)) + 32.0;
  48. println!("{value}{unit} is converted to:{value_k}K and {value_f}F");
  49. println!("Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!");
  50. break;
  51. } else if unit == "F" {
  52. let value_k = (((value - 32.0) * 5.0) / 9.0) + 273.15;
  53. let value_c = ((value - 32.0) * 5.0) / 9.0;
  54. println!("{value}{unit} is Converted to: {value_k}K and {value_c}C");
  55. println!("Thank You for using Temperature-Converter. It was a pleasure to assist you. Byee!!");
  56. break;
  57. };
  58. }
  59. }

答案2

得分: 0

Sure, here is the translated code:

  1. fn main() {
  2. println!("\n欢迎。这是温度转换器\n");
  3. println!("我可以处理华氏度,摄氏度和开尔文");
  4. println!("\n请输入您要转换的值:");
  5. let value: f32 = loop {
  6. let mut value = String::new();
  7. std::io::stdin()
  8. .read_line(&mut value)
  9. .expect("读取失败");
  10. match value.trim().parse::<f32>() {
  11. Ok(num) => break num,
  12. Err(_) => {
  13. println!("请以数字输入值。");
  14. }
  15. };
  16. };
  17. println!("谢谢,我有这个值:{value}。\n请提供它的相应测量单位。");
  18. println!("K:开尔文 C:摄氏度 F:华氏度:");
  19. let unit = loop {
  20. let mut unit = String::new();
  21. std::io::stdin()
  22. .read_line(&mut unit)
  23. .expect("读取失败");
  24. let unit = unit.trim().to_uppercase();
  25. match &*unit {
  26. "K" | "F" | "C" => break unit,
  27. _ => {
  28. println!("请分别输入K,F或C,表示开尔文,华氏度或摄氏度。\n我不区分大小写。");
  29. }
  30. };
  31. };
  32. if unit == "K" {
  33. let value_c = value - 273.15;
  34. let value_f = (((value - 273.15) * 9.0) + (32.0 * 5.0)) / 5.0;
  35. println!("{value}{unit} 转换为:{value_c}摄氏度 和 {value_f}华氏度");
  36. println!("感谢您使用温度转换器。很高兴能帮助您。再见!!");
  37. }
  38. else if unit == "C" {
  39. let value_k = value + 273.15;
  40. let value_f = (value * (9.0 / 5.0)) + 32.0;
  41. println!("{value}{unit} 转换为:{value_k}开尔文 和 {value_f}华氏度");
  42. println!("感谢您使用温度转换器。很高兴能帮助您。再见!!");
  43. }
  44. else if unit == "F" {
  45. let value_k = (((value - 32.0) * 5.0) / 9.0) + 273.15;
  46. let value_c = ((value - 32.0) * 5.0) / 9.0;
  47. println!("{value}{unit} 转换为:{value_k}开尔文 和 {value_c}摄氏度");
  48. println!("感谢您使用温度转换器。很高兴能帮助您。再见!!");
  49. };
  50. }
英文:

Thanks for to user: Kaplan
i have just defined variables with loop and later break the loop with variable value out of the loop.

  1. fn main() {
  2. println!(&quot;\nWelcome. This is Temperature-Converter\n&quot;);
  3. println!(&quot;I work with Fahrenheit, Celsius and Kelvin&quot;);
  4. println!(&quot;\nPlease Input the Value you want to convert: &quot;);
  5. let value:f32 = loop{
  6. let mut value = String::new();
  7. std::io::stdin()
  8. .read_line(&amp;mut value)
  9. .expect(&quot;Failed to read line&quot;);
  10. match value.trim().parse::&lt;f32&gt;(){
  11. Ok(num) =&gt; break num,
  12. Err(_) =&gt; {
  13. println!(&quot;please input value in number(s).&quot;);}
  14. };
  15. };
  16. println!(&quot;Thank You, I have the value: {value}.\nPlease provide it&#39;s respective unit of measurement.&quot;);
  17. println!(&quot;K : for Kelvin C : for Celsius F : for Fahrenheit : &quot;);
  18. let unit = loop{
  19. let mut unit = String::new();
  20. std::io::stdin()
  21. .read_line(&amp;mut unit)
  22. .expect(&quot;Failed to read line&quot;);
  23. let unit = unit.trim().to_uppercase();
  24. match &amp;*unit{
  25. &quot;K&quot; | &quot;F&quot; | &quot;C&quot; =&gt; break unit,
  26. _=&gt;{
  27. println!(&quot;Please input K, F or C for Kelvin, Fahrenheit or Celsius respectively.\nI am not case sensitive.&quot;);
  28. }
  29. };
  30. };
  31. if unit == &quot;K&quot;{
  32. let value_c = value-273.15;
  33. let value_f = (((value-273.15)*9.0)+(32.0*5.0))/5.0;
  34. println!(&quot;{value}{unit} is converted to: {value_c}C and {value_f}F&quot;);
  35. println!(&quot;Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!&quot;);
  36. }
  37. else if unit == &quot;C&quot;{
  38. let value_k = value+273.15;
  39. let value_f = (value*(9.0/5.0))+32.0;
  40. println!(&quot;{value}{unit} is converted to:{value_k}K and {value_f}F&quot;);
  41. println!(&quot;Thank You for using Temperature Conversior. It was a pleasure to assist you. Byee!!&quot;);
  42. }
  43. else if unit == &quot;F&quot;{
  44. let value_k = (((value-32.0)*5.0)/9.0)+273.15;
  45. let value_c = ((value-32.0)*5.0)/9.0;
  46. println!(&quot;{value}{unit} is Converted to: {value_k}K and {value_c}C&quot;);
  47. println!(&quot;Thank You for using Temperature-Converter. It was a pleasure to assist you. Byee!!&quot;);
  48. };
  49. }

huangapple
  • 本文由 发表于 2023年5月18日 06:12:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定