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!");

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: ");

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;
        }

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
}

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()
}

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;
        }
    }
}

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!

Leave a Reply

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

four × 1 =