Rust Programming By Example
eBook - ePub

Rust Programming By Example

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

Buch teilen
  1. 454 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Rust Programming By Example

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

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

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.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Rust Programming By Example als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Rust Programming By Example von Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Informatica & Programmazione in Java. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2018
ISBN
9781788470308

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...

Inhaltsverzeichnis