Masai Mahapa

30 days of Rust - Day one - Hello World

30 days of rust - day one

As a programmer, one needs to always be re-evaluating their knowledge and learning new technologies in order to stay relevant within the industry.

Lately, a language that has come in to my rader is Rust. According to a developer survey by Stack Overflow, it was the most loved 😍 programming language of 2021.

The talk that really got my attention was Ready for Rust by Erik Doernburg. He demonstarted how safe it is by avoiding many critical memory bugs as well as its blazingly fast perfomance. As a javascript developer myself, I thought node was lightning fast. Well, apparently that is only true if all you've ever used is Javascript.😭

I also saw that the Solana blockchain is built with rust, so I believe this will be my path to being a blockchain developer 😎.

Day 1

I headed over to the official Rust website for instructions on how to install it on my pc. I thereafter started reading the free e-book titled The Rust Programming Language.

My first program

I have since wrote my first command line program😝. This converts user input from degrees celcius to degrees farenheit 🌑.

use std::io;

fn main() {
    println!("Temperature converter!");
    loop {
        println!("1. Celcius to Farenheit");
        println!("2. Farenheit to Celcius");
        println!("3. Exit");

        let mut choice = String::new();
        let mut input_value = String::new();
        let output:f32;

        io::stdin()
        .read_line(&mut choice)
        .expect("Failed to read line.");

        let choice: i32 = choice.trim().parse().expect("Not a number");

        if choice == 1 {
            println!("Enter a temperature in celcius:");
            io::stdin()
            .read_line(&mut input_value)
            .expect("Cannot read line");

            let input_value:f32 = input_value.trim().parse().expect("Enter a number");
            output = (input_value *1.8) + 32.0;
            println!("{} degrees celcius converted to Farenheit is : {}", input_value, output);

        } else if choice == 2{
            println!("Enter a temperature in Farenheit:");
            io::stdin()
            .read_line(&mut input_value)
            .expect("Cannot read line");

            let input_value:f32 = input_value.trim().parse().expect("Enter a number");
            output = (input_value -32.0) * (5.0/9.0);
            println!("{} Farenheit converted to degrees celcius  is : {}", input_value, output);
        } else if choice ==3{
            println!("Exiting the program");
            break;
        } else{
            println!("Invalid choice.")
        }
    }
    println!("The end!")   
}

So far so good. I haven't felt this good in quite a while. I am very excited for what lies ahead on this journey.

Share