Rust Programming By Example
eBook - ePub

Rust Programming By Example

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

Compartir libro
  1. 454 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Rust Programming By Example

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

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Rust Programming By Example un PDF/ePUB en línea?
Sí, puedes acceder a Rust Programming By Example de Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante en formato PDF o ePUB, así como a otros libros populares de Informatica y Programmazione in Java. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788470308
Edición
1
Categoría
Informatica

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

Índice