Skip to main content
  1. Posts/

First program in Rust

·303 words·2 mins
Table of Contents

When was the last time you did something for the first time?

Todays was my first time writing a program in rust. For my first assignment it was a simple “Hello what’s your name” followed by a “Hi [name] how are you?”. At first it looked very intimidating and I looked at a reference and was like ‘Why would I need to do this?’ python is extremely easy and less time-consuming. Rust is the complete opposite! Very complicated and very time-consuming this long piece of code for a small task took me an hour, while in python I could do it in less than an minute. But after I completed the task it wasn’t that easy but at the same time it wasn’t too hard. So here’s what I did:

My plan #

  1. We always need to make a small plan in our head.
  2. First we ask ourself how would you get a input/response:
  3. Then we display it with a text that says “Hi [name], how are you?”

I will break down this program so it’s easier to read, basically we start with a function then we use println! with our text inside. Afterwards we create a empty variable using let which creates/defines a variable and mut because by default in rust you can’t change the value in the variable therefore mut allows us to change the variable anytime. Next we use a library using std::io; using the method .read_line() and if something goes wrong it will print a error message that reads “Oh noes!”. And lastly we print a message that says “Hi [name], how ya doin.”

Here’s what it should look like:

use std::io;

fn main() {
    println!("Hello, what is your name?");

    let mut name = String::new();
    io::stdin()
        .read_line(&mut name)
        .expect("Oh noes!");

    println!("Hi {}, how ya doin.", name.trim_end());
}

BYE :333333333!S