Trying to use the ‘match’ statement.

Below is my code:
I am trying to have a default error message but rust does not like it. The match variable is i32 how can I send a default message?
Any suggestions would be most helpful.

Error is on line 16.

fn main() {

println!(“Please selec…


This content originally appeared on DEV Community and was authored by Jack Schuster

Below is my code:
I am trying to have a default error message but rust does not like it. The match variable is i32 how can I send a default message?

Any suggestions would be most helpful.

Error is on line 16.

fn main() {

    println!("Please select the conversion type:");
    println!("1. Fahrenheit to Celsius");
    println!("2. Celsius to Fahrenheit");

    let mut conversion_type = String::new();
    io::stdin().read_line(&mut conversion_type)
        .expect("Failed to read line");
    let conversion_type = conversion_type.trim();
    let conversion_type = match conversion_type {
        "1" => 1,
        "2" => 2,
        "3" => 3,
         _ => { println!("Please input 1 or 2!")} //gives error in rust

    };

    println!("The converted temperature is {}", conversion_type);

}
Regards,
Jack


This content originally appeared on DEV Community and was authored by Jack Schuster


Print Share Comment Cite Upload Translate Updates
APA

Jack Schuster | Sciencx (2025-01-11T20:50:39+00:00) Trying to use the ‘match’ statement.. Retrieved from https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/

MLA
" » Trying to use the ‘match’ statement.." Jack Schuster | Sciencx - Saturday January 11, 2025, https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/
HARVARD
Jack Schuster | Sciencx Saturday January 11, 2025 » Trying to use the ‘match’ statement.., viewed ,<https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/>
VANCOUVER
Jack Schuster | Sciencx - » Trying to use the ‘match’ statement.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/
CHICAGO
" » Trying to use the ‘match’ statement.." Jack Schuster | Sciencx - Accessed . https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/
IEEE
" » Trying to use the ‘match’ statement.." Jack Schuster | Sciencx [Online]. Available: https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/. [Accessed: ]
rf:citation
» Trying to use the ‘match’ statement. | Jack Schuster | Sciencx | https://www.scien.cx/2025/01/11/trying-to-use-the-match-statement/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.