Rust – il linguaggio – 2

unnContinuo da qui l’esame di Rust, oggi qui: /usr/local/share/doc/rust/html/book/getting-started.html. Si procede oltre, con le dritte per l’installazione, già fatta ma vediamo…
Ci sono 3 livelli (tier) di supporto, con Linus sono OK 😀
OK, avanti, non nomino gli URL, sono di corsa 😉

Volendo c’è un’IDE, SolidOak, per adesso non l’installo, preferisco fare come una volta. In realtà per Python uso in genere Geany e per Racket DrRacket; quest’ultimo è davvero eccezionale, quai indispensabile.

Esaminiamo in dettaglio main.rs, quello di Hello world!.

fn main() {
    println!("Hello, world!");
}

L’estensione per i files sorgenti è .rs e si compila con rustc [file].rs ottenendo [file] come eseguibile.

Si inizia sempre con fn main() { e si finisce con }. fn definisce una funzione. Se ci sono argomenti si –come al solito.

println!("Hello, world!");

Per la riga usare 4 spazi e non tab 😀
println!() è una macro, come si vede dal !.
Correzione: il ! indica che la funzione println!() non ritorna un valore, come raccontato qui.
Rust implements println! as a macro rather than a function for good reasons, but that’s an advanced topic. One last thing to mention: Rust’s macros are significantly different from C macros, if you’ve used those. Don’t be scared of using macros. We’ll get to the details eventually, you’ll just have to take it on trust for now.
Non dimenticare il ; che termina l’espressione; Rust è fatto da espressioni, non linee.

rustc è troppo semplice, quando il progetto diventa interessante si passa a Cargo.

Cargo is a tool that Rustaceans use to help manage their Rust projects. Cargo is currently in a pre-1.0 state, and so it is still a work in progress. However, it is already good enough to use for many Rust projects, and so it is assumed that Rust projects will use Cargo from the beginning.

rs2-0

Cargo manages three things: building our code, downloading the dependencies our code needs, and building those dependencies. At first, our program doesn’t have any dependencies, so we’ll only be using the first part of its functionality. Eventually, we’ll add more. Since we started off by using Cargo, it’ll be easy to add later.

Let’s convert Hello World to Cargo. Ci sono un po’ di convenzioni da seguire, mi adeguo e faccio tutto scrupolosamente 🙄

To Cargo-ify our project, we need to do three things: Make a Cargo.toml configuration file, put our source file in the right place, and get rid of the old executable (main).

Note: since we’re creating an executable, we retain main.rs as the source filename. If we want to make a library instead, we should use lib.rs. This convention is used by Cargo to successfully compile our projects, but it can be overridden if we wish. Custom file locations for the entry point can be specified with a [lib] or [[bin]] key in the TOML file.

Cargo expects our source files to live inside a src directory. That leaves the top level for other things, like READMEs, license information, and anything not related to our code. Cargo helps us keep our projects nice and tidy. A place for everything, and everything in its place.

Next, our configuration file:

re2-1

This file is in the TOML format. TOML is similar to INI, but has some extra goodies. According to the TOML docs,

TOML aims to be a minimal configuration file format that’s easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.

Once we have this file in place in our project’s root directory, we should be ready to build! To do so, run:

rs2-2

Si può semplificare build + esecuzione con

rs2-3

Notice that we didn’t re-build the project this time. Cargo figured out that we hadn’t changed the source file, and so it just ran the binary. OK, come make già lo scorso millennio, dai 🙄
Naturalmente i vantaggi si vedranno quando il progetto cresce.
When our project is finally ready for release, we can use cargo build --release to compile our project with optimizations.

rs2-4

You’ll also notice that Cargo has created a new file: Cargo.lock:

rs2-5

That’s it! We’ve successfully built hello_world with Cargo. Even though our program is simple, it’s using much of the real tooling that we’ll use for the rest of our Rust career. We can expect to do this to get started with virtually all Rust projects. Volendo si può a questo punto git-arlo.

Adesso un nuovo progetto.

We don’t have to go through this whole process every time we want to start a new project! Cargo has the ability to make a bare-bones project directory in which we can start developing right away.

To start a new project with Cargo, we use cargo new:

rs2-6

We’re passing --bin because our goal is to get straight to making an executable application, as opposed to a library. Executables are often called ‘binaries.’
Let’s check out what Cargo has generated for us:

rs2-7

Quasi OK, ha perso parte della stringa authors 😉
Cargo has generated a “Hello World!” for us, and we’re ready to start coding! Cargo has its own guide which covers Cargo’s features in much more depth.

Now that we’ve got the tools down, let’s actually learn more about the Rust language itself. These are the basics that will serve us well through the rest of our time with Rust.

You have two options: Dive into a project with ‘Learn Rust’, or start from the bottom and work your way up with ‘Syntax and Semantics’. More experienced systems programmers will probably prefer ‘Learn Rust’, while those from dynamic backgrounds may enjoy either. Different people learn differently! Choose whatever’s right for you.

OK, vado con Learn Rust, ma non subito, prossimamente 😀

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

  • Rust – il linguaggio – 3 | Ok, panico su 20 gennaio 2016 alle 09:57

    […] qui /usr/local/share/doc/rust/html/book/learn-rust.html continuando da qui. Però, dopo una rapida occhiata cambio idea, meglio fare le cose per bene, formalmente, […]

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: