Rust Programming By Example
eBook - ePub

Rust Programming By Example

Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante

Partager le livre
  1. 454 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Rust Programming By Example

Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Discover the world of Rust programming through real-world examplesAbout This Book‱ Implement various features of Rust to build blazingly fast applications‱ Learn to build GUI applications using Gtk-rs‱ Explore the multi-threading aspect of Rust to tackle problems in concurrency and in distributed environmentsWho This Book Is ForThis book is for software developers interested in system level and application programming who are looking for a quick entry into using Rust and understanding the core features of the Rust Programming. It's assumed that you have a basic understanding of Java, C#, Ruby, Python, or JavaScript.What You Will Learn‱ Compile and run the Rust projects using the Cargo-Rust Package manager‱ Use Rust-SDL features such as the event loop, windows, infinite loops, pattern matching, and more‱ Create a graphical interface using Gtk-rs and Rust-SDL‱ Incorporate concurrency mechanism and multi-threading along with thread safety and locks‱ Implement the FTP protocol using an Asynchronous I/O stack with the Tokio libraryIn DetailRust is an open source, safe, concurrent, practical language created by Mozilla. It runs blazingly fast, prevents segfaults, and guarantees safety. This book gets you started with essential software development by guiding you through the different aspects of Rust programming. With this approach, you can bridge the gap between learning and implementing immediately. Beginning with an introduction to Rust, you'll learn the basic aspects such as its syntax, data types, functions, generics, control flows, and more. After this, you'll jump straight into building your first project, a Tetris game. Next you'll build a graphical music player and work with fast, reliable networking software using Tokio, the scalable and productive asynchronous IO Rust library. Over the course of this book, you'll explore various features of Rust Programming including its SDL features, event loop, File I/O, and the famous GTK+ widget toolkit. Through these projects, you'll see how well Rust performs in terms of concurrency—including parallelism, reliability, improved performance, generics, macros, and thread safety. We'll also cover some asynchronous and reactive programming aspects of Rust.By the end of the book, you'll be comfortable building various real-world applications in Rust.Style and approachThis comprehensive guide uses practical examples to implement various RUST programming features.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Rust Programming By Example est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Rust Programming By Example par Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Informatica et Programmazione in Java. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788470308
Édition
1

Events and Basic Game Mechanisms

In the last chapter, we saw how to add dependencies into a project thanks to Cargo and the basics of the SDL2 library.
We now have all the Rust basics in order to write the Tetris game. Time to look at how we will actually write Tetris.
In this chapter, we will cover the following topics:
  • Tetrimino
  • Creating tetriminos
  • Generating a tetrimino
  • Tetris struct
  • Interacting with the game map
  • SDL events
  • Score, level, lines sent

Writing Tetris

First, let's review the Tetris rules (just in case):
  • There is a grid with a height of 16 blocks and a width of 10 blocks.
  • You have seven different tetrimino (a tetris piece) that are all composed of four blocks.
  • A new tetrimino appears at the top of the game's grid every time the previous one cannot descend any more (because the block below is already occupied or because you've reached the game's floor).
  • The game is over when a new tetrimino cannot appear anymore (because there is already a tetrimino at the top of the grid).
  • Every time a line is full (all blocks are occupied by a tetrimino part), it disappears and all lines above descend by one line.
Now that we all agree on the game rules, let's see how to actually write those mechanisms.
First, we need to actually create those tetriminos.

Tetrimino

As said previously, every tetrimino has four blocks. Another thing to note is that they can rotate. So for example you have this tetrimino:
Figure 3.1
It can also rotate in the three following positions:
Figure 3.2
Theoretically, every tetrimino should have four states, but in reality, not all of them do. For example, this one has no transformation so to speak:
Figure 3.3
And these three only have two states:
Figure 3.4
We have two ways of handling these rotations: using matrix rotation or storing the different states. To have a code that's easy to read and update, I picked the second option, but don't hesitate to try using matrix on your own, it could help you learn a lot of new things!
So first, let's write down a struct for tetriminos:
struct Tetrimino { states: Vec<Vec<Vec<u8>>>, x: isize, y: usize, current_state: u8, }
Everything seems fine except this line:
states: Vec<Vec<Vec<u8>>>,
Pretty ugly, right? Let's make it look a bit better by using type aliasing!
So what is our states field representing? Simply a list of states. Each state repres...

Table des matiĂšres