Getting started with Rust

I believe that, from time to time, it is interesting to learn a new language or framework that takes us out of our comfort zone. So I decided to learn Rust (Ok! I confess I was influenced by Ayende).

Why Rust?

Programs written in Rust are blazingly fast and memory-efficient. Rust programs are memory-safe and thread-safe. Also, the compiler is excellent and help us to fix bugs quickly.

Another good reason? Hundreds of companies have adopted rust.

Finally, it is nice to learn a language that really challenges C++.

How to start?

If like me, you know nothing about Rust, I strongly recommend you to read the excellent “The Rust Programming Language” book which is available for free online.

The first chapter will teach you how to get it and to write your first program.

The following code comes from the book, by the way.

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1, 101);

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();

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

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        println!("You guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater => println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }
}

Even simple, this program shows features that I loved like immutability, variable name shadowing, pattern matching and more.

It is time to move on

In future posts I will solve classic computer science problems using Rust.

Stay tuned!

Compartilhe este insight:

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Há muito valor em saber como fazer as coisas. Entretanto, antes, é preciso saber o porquê. Conheço excelentes profissionais, profundos...
Parsing large files is a recurring and challenging task. Right? It is too easy to write slow code that consumes...
Aqui, um registro da apresentação que realizei na abertura do IoT Weekend. O que você acha do que foi dito?
In this post, I will show how to do your first steps with OpenCV quickly using Visual Studio 2017 and...
It’s time to start learning RavenDB 4. Even if you know previous versions of RavenDB, you will probably get good...
Neste post mostro como implementar um EventBus, utilizando RabbitMQ, em C#. Este código ainda está em desenvolvimento. Em breve, uma...
× Precisa de ajuda?