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:

If you ask me one tip to improve the performance of your applications, it would be: Design your objects to...
Este post foi originalmente publicado em 2015 (infelizmente, a postagem original não está mais disponível) Nesse post vamos implementar, passo-a-passo,...
As an experienced .NET developer, you have to deal with NullReferenceException occurrences every day. Am I right? I firmly believe...
Este exemplo é inspirado no livro do Ayende Se você deseja aprender RavenDB, recomendo que se inscreva no RavenDB bootcamp...
Conheci o poema maravilhoso da Viviane Mosé, transcrito abaixo, na interpretação de uma grande amiga. Quem tem olhos pra ver...
In this post, I will help you to set up your first RavenDB cluster. To be honest, it is not...