# With Simple Programming Language ```rust // This program will convert a temperature from Celsius to Fahrenheit and vice versa. // import necessary modules use std::io; fn main() { let temperature: f64 = get_number(); // Call the function to get a number from the user let unit: String = get_unit(); // Call the function to get the unit from the user // Check if the unit is valid if unit == "C" || unit == "F" { result_converted(temperature, unit); // Call the function to print the converted result } else { println!("Invalid unit. Please enter C for Celsius or F for Fahrenheit."); // Print an error message if the unit is invalid } } fn result_converted(temperature: f64, unit: String) { // Function to print the converted result if unit == "C" { let fahrenheit: f64 = celsius_to_fahrenheit(temperature); // Convert Celsius to Fahrenheit println!("{}°C is equal to {:.2}°F", temperature, fahrenheit); // Print the result } else if unit == "F" { let celsius: f64 = fahrenheit_to_celsius(temperature); // Convert Fahrenheit to Celsius println!("{}°F is equal to {:.2}°C", temperature, celsius); // Print the result } } fn get_number() -> f64 { // Get a valid number from the user loop { // Loop until a valid number is entered println!("Enter the temperature: "); // Prompt the user for input let mut input = String::new(); // Create a mutable string to store the input io::stdin().read_line(&mut input).expect("Failed to read line"); // Read a line from standard input match input.trim().parse::<f64>() { // Try to parse the input as a f64 Ok(num) => return num, // If successful, return the number Err(_) => println!("Invalid input. Please enter a valid number."), // Print an error message if parsing fails } } } fn get_unit() -> String { // Function to get a valid unit (C or F) from the user loop { // Loop until a valid unit is entered println!("Enter the unit (C for Celsius, F for Fahrenheit): "); // Prompt the user for input let mut input = String::new(); // Create a mutable string to store the input io::stdin().read_line(&mut input).expect("Failed to read line"); // Read a line from standard input let unit = input.trim().to_uppercase(); // Convert the input to uppercase and trim whitespace if unit == "C" || unit == "F" { // Check if the unit is valid return unit; // If valid, return the unit } else { println!("Invalid unit. Please enter C for Celsius or F for Fahrenheit."); // Print an error message if the unit is invalid } } } // Define a function to convert Celsius to Fahrenheit fn celsius_to_fahrenheit(celsius: f64) -> f64 { return (celsius * 9.0 / 5.0) + 32.0; } // Define a function to convert Fahrenheit to Celsius fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 { return (fahrenheit - 32.0) * 5.0 / 9.0; } ``` # With More Complicated Language ```rust // This program will convert a temperature from Celsius to Fahrenheit and vice versa. // import necessary modules use std::io; // Define an enum for temperature units for better type safety enum TemperatureUnit { Celsius, Fahrenheit, } fn main() { let temperature: f64 = get_number(); // Call the function to get a number from the user let unit: TemperatureUnit = get_unit(); // Call the function to get the unit from the user display_conversion_result(temperature, unit); // Call the function to print the converted result } fn display_conversion_result(temperature: f64, unit: TemperatureUnit) { // Function to print the converted result using a match statement match unit { TemperatureUnit::Celsius => { let fahrenheit: f64 = celsius_to_fahrenheit(temperature); // Convert Celsius to Fahrenheit println!("{}°C is equal to {:.2}°F", temperature, fahrenheit); // Print the result } TemperatureUnit::Fahrenheit => { let celsius: f64 = fahrenheit_to_celsius(temperature); // Convert Fahrenheit to Celsius println!("{}°F is equal to {:.2}°C", temperature, celsius); // Print the result } } } fn get_number() -> f64 { // Get a valid number from the user loop { // Loop until a valid number is entered println!("Enter the temperature: "); // Prompt the user for input let mut input = String::new(); // Create a mutable string to store the input io::stdin().read_line(&mut input).expect("Failed to read line"); // Read a line from standard input match input.trim().parse::<f64>() { // Try to parse the input as a f64 Ok(num) => return num, // If successful, return the number Err(_) => println!("Invalid input. Please enter a valid number."), // Print an error message if parsing fails } } } fn get_unit() -> TemperatureUnit { // Function to get a valid unit (C or F) from the user loop { // Loop until a valid unit is entered println!("Enter the unit (C for Celsius, F for Fahrenheit): "); // Prompt the user for input let mut input = String::new(); // Create a mutable string to store the input io::stdin().read_line(&mut input).expect("Failed to read line"); // Read a line from standard input // Convert the input to uppercase, trim whitespace, and match against known units match input.trim().to_uppercase().as_str() { "C" => return TemperatureUnit::Celsius, // If "C", return Celsius enum variant "F" => return TemperatureUnit::Fahrenheit, // If "F", return Fahrenheit enum variant _ => println!("Invalid unit. Please enter C for Celsius or F for Fahrenheit."), // Print an error message if the unit is invalid } } } // Define a function to convert Celsius to Fahrenheit fn celsius_to_fahrenheit(celsius: f64) -> f64 { (celsius * 9.0 / 5.0) + 32.0 // The `return` keyword is optional for the last expression } // Define a function to convert Fahrenheit to Celsius fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 { (fahrenheit - 32.0) * 5.0 / 9.0 // The `return` keyword is optional for the last expression } ```