Rust Programming By Example | Programming | Paperback (original) (raw)
Rust Programming By Example: Enter the world of Rust by building engaging, concurrent, reactive, and robust applications
Subscription Free Trial Renews at $19.99p/m
What do you get with Print?
Instant access to your digital copy whilst your Print order is Shipped
Paperback book shipped to your preferred address
Redeem a companion digital copy on all Print orders
Access this title in our online reader with advanced features
DRM FREE - Read whenever, wherever and however you want
OR
Contact Details
Payment Processing...
Completed
Shipping Address
Billing is same as shipping information
Billing Address
View table of contents
Preview Book
Before starting to write the Tetris, a few things remain to be talked about, such as crates, which we'll be using a lot (and you'll be using a lot as well once you're rusting on your own!). Let's start with crates!
In Rust, packages (both binaries and libraries) are named crates. You can find a lot of them on crates.io. Today, we'll use the SDL2 crate in order to make our tetris, but before even thinking about this, we need to install the SDL2 library that is used by the SDL2 crate!
Before going any further, we need to install the SDL library.
Depending on your package management tool, run the following to install SDL2 on Linux:
apt package mananger:
$ sudo apt-get install libsdl2-dev
dnf package manager:
$ sudo dnf install SDL2-devel
yum package manager:
Once done, your SDL2 installation is ready!
To install SDL2 on Mac, Simply run the following:
You're good to go!
...
The Rust package manager, cargo, allows us to create a new project very easily with just one command, cargo new. Let's run it as follow:
You should have a new folder tetris containing the following:
tetris/
|
|- Cargo.toml
|- src/
|
|- main.rs
Note that if you ran cargo new without the --bin flag, then you will have a lib.rs file instead of main.rs.
Now write this into your Cargo.toml file:
[package]
name = "tetris"
version = "0.0.1"
[dependencies]
sdl2 = "0.30.0"
Here, we declare that our project's name is tetris, its version is 0.0.1 (it isn't really important at the moment), and that it has a dependency on the sdl2 crate.
For the versioning, Cargo follows SemVer (Semantic Versioning). It works as follows:
[major...
Before going any further, we need to talk about how file hierarchy works in Rust through its modules.
The first thing to know is that files and folders are handled as modules in Rust. Consider the following:
|- src/ | |- main.rs |- another_file.rs
If you want to declare that a module is in the another_file.rs file, you'll need to add to your main.rs file:
You will now have access to everything contained in another_file.rs (as long as it's public).
Another thing to know: you can only declare modules whose files are on the same level as your current module/file. Here's a short example to sum this up:
|- src/ | |- main.rs |- subfolder/ |- another_file.rs
If you try to declare a module referring to another_file.rs directly into main.rs, as shown preceding, it'll fail because there...
Okay, we're now ready to start writing down our tetris!
First, let's fulfill our main.rs file in order to check whether everything is working as expected:
extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2:⌨️:Keycode;
use std::time::Duration;
use std::thread::sleep;
pub fn main() {
let sdl_context = sdl2::init().expect("SDL initialization
failed");
let video_subsystem = sdl_context.video().expect("Couldn't get
SDL video subsystem");
let window = video_subsystem.window("rust-sdl2 demo: Video", 800,
600)
.position_centered()
.opengl()
.build()
.expect("Failed to create window");
let mut canvas = window.into_canvas().build().expect("Failed to
convert window into...
The previous example created a window and drew into it. Now let's see how it did that!
Before going any further, we need to import the SDL2 crate, as follows:
With this, we now have access to everything it contains.
Now that we've imported sdl2, we need to initialize an SDL context:
let sdl_context = sdl2::init().expect("SDL initialization failed");
Once done, we need to get the video subsystem:
let video_subsystem = sdl_context.video().expect("Couldn't get SDL
video subsystem");
We can now create the window:
let window = video_subsystem.window("Tetris", 800, 600)
.position_centered()
.opengl()
.build()
.expect("Failed to create window");
A few notes on...
We now have a working window; it'd be nice to draw into it. First, we need to get the window's canvas before starting the main loop:
let mut canvas = window.into_canvas()
.target_texture()
.present_vsync()
.build()
.expect("Couldn't get window's canvas");
A few explanations for the preceding code:
- into_canvas transforms the window into a canvas so that we can manipulate it more easily
- target_texture activates texture rendering support
- present_vsync enables the v-sync (also known as vertical-synchronization) limit
- build creates the canvas by applying all previously set parameters
Then we'll create a texture that we'll paste onto the window's canvas. First, let's get the texture creator, but before that, add this include...
Understanding Rust crates
In Rust, packages (both binaries and libraries) are named crates. You can find a lot of them on crates.io
. Today, we'll use the SDL2 crate in order to make our tetris, but before even thinking about this, we need to install the SDL2
library that is used by the SDL2
crate!
Installing SDL2
Before going any further, we need to install the SDL library.
Installing SDL2 on Linux
Depending on your package management tool, run the following to install SDL2 on Linux:
apt package mananger
:
$ sudo apt-get install libsdl2-dev
dnf package manager
:
$ sudo dnf install SDL2-devel
yum package manager
:
$ yum install SDL2-devel
Once done, your SDL2 installation is ready!
Installing SDL2 on Mac
To install SDL2 on Mac, Simply run the following:
$ brew install sdl2
You're good to go!
Installing SDL2 on Windows
All these installation instructions come directly from the Rust SDL2 crate.
Windows with Build Script
A few steps will be required in order to make all of it work. Follow the guide!
Download the
mingw
andmsvc
development libraries from http://www.libsdl.org/ (SDL2-devel-2.0.x-mingw.tar.gz
andSDL2-devel-2.0.x-VC.zip
).Unpack to folders of your choice. (You can delete it afterward.)
Create the following folder structure in the same folder as your
Cargo.toml
:gnu-mingw\dll\32 ...
Page1 of 10
Key benefits
- • 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 environments
Description
Rust 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.
Who is this book for?
This 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 library
Estimated delivery fee Deliver to United States
Economy delivery 10 - 13 business days
Free $6.95
Premium delivery 6 - 9 business days
$21.95
(Includes tracking information)
Publication date : Jan 11, 2018
Length: 454 pages
Edition : 1st
Language : English
ISBN-13 : 9781788390637
What do you get with Print?
Instant access to your digital copy whilst your Print order is Shipped
Paperback book shipped to your preferred address
Redeem a companion digital copy on all Print orders
Access this title in our online reader with advanced features
DRM FREE - Read whenever, wherever and however you want
OR
Contact Details
Payment Processing...
Completed
Shipping Address
Billing is same as shipping information
Billing Address
Estimated delivery fee Deliver to United States
Economy delivery 10 - 13 business days
Free $6.95
Premium delivery 6 - 9 business days
$21.95
(Includes tracking information)
Publication date : Jan 11, 2018
Length: 454 pages
Edition : 1st
Language : English
ISBN-13 : 9781788390637