Rust Programming: Build a Temperature Converter with Functions and Loops

Rust

Introduction

Rust programming is known for its performance, safety, and modern features that make it an increasingly popular choice for systems programming. If you’re new to Rust, one of the best ways to get acquainted with its syntax and functionalities is by building small, practical applications. In this tutorial, we’ll create a simple Rust program that converts temperatures between Celsius and Fahrenheit. This project will help you understand fundamental Rust concepts such as functions, loops, user input, and type conversions.

This guide is perfect for beginners who want to dive into Rust with a hands-on project. Along the way, we’ll explain the code in detail so you can learn the language step by step.

Prerequisites

Before starting, make sure you have Rust installed on your machine. You can install Rust using rustup, which is the recommended way to install Rust. To learn more about Rust, visit the official Rust website. If you haven’t set up your Rust environment yet, check out the Cargo documentation for details on how to get started.

The Program: Temperature Converter

Our program will allow users to choose between converting from Celsius to Fahrenheit or from Fahrenheit to Celsius. The program will continue running until the user decides to quit. Here’s the complete code, which we’ll break down in the following sections.

The Complete Code

use std::io;

fn main() {
    println!("Hello, Rust Chapter 3!");

    loop {
        let what_to_do = get_user_input("Enter 1 to convert from Celsius to Fahrenheit, or 2 to convert from Fahrenheit to Celsius: ");

        if what_to_do == 1.0 {
            let celsius = get_user_input("Enter the temperature in Celsius: ");
            let fahrenheit = convert_to_fahrenheit(celsius);
            println!("{} Celsius is {} Fahrenheit.", celsius, fahrenheit);
        } else if what_to_do == 2.0 {
            let fahrenheit = get_user_input("Enter the temperature in Fahrenheit: ");
            let celsius = convert_to_celsius(fahrenheit);
            println!("{} Fahrenheit is {} Celsius.", fahrenheit, celsius);
        } else {
            println!("Invalid input. Please enter 1 or 2.");
            continue;
        }

        let continue_input = get_user_input_str("Do you want to continue converting? (y/n): ");
        if continue_input.trim().eq_ignore_ascii_case("n") {
            println!("Goodbye!");
            break;
        }
    }
}

fn convert_to_celsius(fahrenheit: f32) -> f32 {
    (fahrenheit - 32.0) * 5.0 / 9.0
}

fn convert_to_fahrenheit(celsius: f32) -> f32 {
    celsius * 9.0 / 5.0 + 32.0
}

fn get_user_input(prompt: &str) -> f32 {
    loop {
        let mut input = String::new();
        println!("{}", prompt);
        io::stdin().read_line(&mut input).expect("Failed to read line");
        match input.trim().parse() {
            Ok(num) => return num,
            Err(_) => println!("Invalid input. Please enter a valid number."),
        }
    }
}

fn get_user_input_str(prompt: &str) -> String {
    let mut input = String::new();
    println!("{}", prompt);
    io::stdin().read_line(&mut input).expect("Failed to read line");
    input.trim().to_string()
}

Step-by-Step Breakdown

1. Imports and the main Function

use std::io;

fn main() {
    println!("Hello, Rust Chapter 3!");
  • use std::io;: This line imports Rust’s standard input/output library, which we’ll use to handle user input.
  • fn main(): The main function is the entry point of every Rust program. Here, we start by printing a greeting message using println!.

2. Infinite Loop and User Choice

    loop {
        let what_to_do = get_user_input("Enter 1 to convert from Celsius to Fahrenheit, or 2 to convert from Fahrenheit to Celsius: ");
  • loop {}: This is an infinite loop. The program will repeatedly ask the user for input until they decide to quit.
  • get_user_input(...): We call a helper function (defined later) to get the user’s choice. This function reads input and returns it as a f32 (floating-point number).

3. Handling User Input

        if what_to_do == 1.0 {
            let celsius = get_user_input("Enter the temperature in Celsius: ");
            let fahrenheit = convert_to_fahrenheit(celsius);
            println!("{} Celsius is {} Fahrenheit.", celsius, fahrenheit);
        } else if what_to_do == 2.0 {
            let fahrenheit = get_user_input("Enter the temperature in Fahrenheit: ");
            let celsius = convert_to_celsius(fahrenheit);
            println!("{} Fahrenheit is {} Celsius.", fahrenheit, celsius);
        } else {
            println!("Invalid input. Please enter 1 or 2.");
            continue;
        }
  • if/else if/else Statements: These statements determine what action to take based on the user’s input.
    • If the user enters 1, the program converts Celsius to Fahrenheit.
    • If the user enters 2, it converts Fahrenheit to Celsius.
    • If the input is invalid (neither 1 nor 2), the program prompts the user again.

4. Converting Temperatures

fn convert_to_celsius(fahrenheit: f32) -> f32 {
    (fahrenheit - 32.0) * 5.0 / 9.0
}

fn convert_to_fahrenheit(celsius: f32) -> f32 {
    celsius * 9.0 / 5.0 + 32.0
}
  • These functions perform the actual temperature conversion.
  • convert_to_celsius: Converts Fahrenheit to Celsius using the formula (Fahrenheit - 32) * 5/9.
  • convert_to_fahrenheit: Converts Celsius to Fahrenheit using the formula Celsius * 9/5 + 32.

5. Getting User Input

fn get_user_input(prompt: &str) -> f32 {
    loop {
        let mut input = String::new();
        println!("{}", prompt);
        io::stdin().read_line(&mut input).expect("Failed to read line");
        match input.trim().parse() {
            Ok(num) => return num,
            Err(_) => println!("Invalid input. Please enter a valid number."),
        }
    }
}

fn get_user_input_str(prompt: &str) -> String {
    let mut input = String::new();
    println!("{}", prompt);
    io::stdin().read_line(&mut input).expect("Failed to read line");
    input.trim().to_string()
}
  • get_user_input: This function repeatedly prompts the user until they enter a valid number.
    • String::new(): Creates a new, empty String to hold the user’s input.
    • io::stdin().read_line(&mut input): Reads a line of input from the user.
    • input.trim().parse(): Attempts to parse the trimmed input into a floating-point number (f32).
    • match input.trim().parse(): If parsing is successful, it returns the number; otherwise, it prints an error message and prompts again.
  • get_user_input_str: Similar to get_user_input, but this function returns a String instead of a number. It’s used for handling textual inputs, like “y” or “n”.

6. Continuing or Exiting the Loop

        let continue_input = get_user_input_str("Do you want to continue converting? (y/n): ");
        if continue_input.trim().eq_ignore_ascii_case("n") {
            println!("Goodbye!");
            break;
        }
    }
}
  • After each conversion, the program asks the user if they want to continue.
  • eq_ignore_ascii_case("n"): Checks if the user entered “n” (case insensitive). If so, the loop breaks, and the program ends with a “Goodbye!” message.

Conclusion

By following this tutorial, you’ve built a simple yet functional temperature converter in Rust programming. Through this process, you’ve learned about Rust’s syntax, functions, loops, conditional statements, and user input handling. This project is a great way to get your feet wet with Rust and understand its fundamental concepts.

If you want to learn more about Rust programming, check out the official Rust documentation, which is an excellent resource for beginners and experienced developers alike. Keep experimenting, and don’t hesitate to modify this program to explore other Rust features!

Happy coding!

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

fifteen + fifteen =