Rust Programming By Example
eBook - ePub

Rust Programming By Example

  1. 454 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

About this book

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.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Rust Programming By Example by Guillaume Gomez, Antoni Boucher, Sebastian Dröge, Daniel Durante in PDF and/or ePUB format, as well as other popular books in Computer Science & Hardware. We have over one million books available in our catalogue for you to explore.

Information

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 of contents

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Basics of Rust
  7. Starting with SDL
  8. Events and Basic Game Mechanisms
  9. Adding All Game Mechanisms
  10. Creating a Music Player
  11. Implementing the Engine of the Music Player
  12. Music Player in a More Rusty Way with Relm
  13. Understanding FTP
  14. Implementing an Asynchronous FTP Server
  15. Implementing Asynchronous File Transfer
  16. Rust Best Practices
  17. Other Books You May Enjoy