Skip to Content
Rust Programming By Example
book

Rust Programming By Example

by Guillaume Gomez, Antoni Boucher
January 2018
Beginner to intermediate
454 pages
10h 8m
English
Packt Publishing
Content preview from Rust Programming By Example

Creating tetriminos

We wrote the type that will be used in our game, but we didn't write its initialization/creation yet. This is where Rust traits will be useful.

Let's start by writing a generator trait that will be implemented on all tetriminos:

trait TetriminoGenerator {
    fn new() -> Tetrimino;
}

And that's it. This trait just provides a function that creates a new Tetrimino instance. It maybe doesn't like this very much, but thanks to this trait, we'll be able to easily create all our tetriminos.

Time to write our first tetrimino:

struct TetriminoI;

No need to look for more code, this is what a tetrimino really looks like. It's an empty structure. The interesting part comes just after:

impl TetriminoGenerator for TetriminoI {
    fn new() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Programming Cookbook

Rust Programming Cookbook

Claus Matzinger
Rust Web Programming

Rust Web Programming

Maxwell Flitton

Publisher Resources

ISBN: 9781788390637Supplemental Content