Hands-on Rust Source Code
This repository contains the source code for the examples found in Hands-on Rust. These are also available from my publisher, PragProg and are shared with their permission.
The examples included are:
Installing Rust
This code covers the Rust and Your Development Environment chapter. It is designed to help you get a working Rust toolchain together, and understand the tools Rust provides.
- Hello World---the obligatory first Rust program we all encounter.
- Clippy---an example showing
Clippy
in action, finding a common issue. - Clippy Fixed---the Clippy example, now working without suggestions.
First Steps with Rust
This code covers the First Steps with Rust chapter. It is designed to teach language basics.
- Hello Your Name---First example, prompting the user for their name, storing it in a variable and printing it.
- Hello Your Name Function---Move obtaining names into a function.
- Trimming - The Problem---Demonstrates how
\n
and other control codes can be captured in standard input. - Trimming---Demonstrates how to sanitize user input.
- Treehouse Guestlist---teaches how to store data in an array and search it.
- Treehouse Structs---learn to store guest information in a struct, and iterate/search arrays of structs.
- Treehouse Vectors---expand the treehouse by using a
Vec
for guest storage. - Treehouse Enums---learn to use Rust's
enum
system, andmatch
on results.
Build Your First Game with Rust
This code covers the Build Your First Game with Rust chapter. It takes the knowledge from the previous two chapters and helps you make your first game, Flappy Dragon.
- Hello, Bracket Terminal---import that
bracket-lib
crate, learn about dependencies and writing to the screen. - Flappy States---learn to manage the overall structure of the game with states.
- Flappy Player---add a player to the game, flapping on command.
- Flappy Dragon---your first playable game, an ASCII mode Flappy Dragon game.
- Flappy Bonus---Bonus content, showing Flappy Dragon with graphics and smooth movement.
Flappy Bonus has an accompanying bonus online tutorial.
Build a Dungeon Crawler
This code covers the Build a Dungeon Crawler chapter. It starts a new game that will form the basis of the remainder of the book.
- Dungeon Map---helps you organize your code into modules, build a blank map, and understand tile map storage.
- Dungeon Player---adds a player who can walk around the map, introducing game-loop based input handling, state management and interactions between structures.
- Dungeon Rooms---introduces using algorithms to build a basic dungeon map, and the concept of a map builder API.
- Dungeon Graphics---adds tile-based rendering to the game, giving you a dungeon you can walk around.
Compose Dungeon Denizens
This code covers the Compose Dungeon Denizens chapter. It introduces the Entity-Component System architecture and focuses on composing dungeon elements from reusable parts.
- Player ECS---converts the simple player handler to use entities, components and systems. Introduces multi-file modules and using procedural macros. Demonstrates fearless concurrency, and how easy Rust can make it to benefit from concurrency.
- ECS Monsters---walks you through using the same systems to support monsters in your game. Adds initial collision detection, allowing you to kill monsters by walking into them.
Take Turns with the Monsters
This code covers the Take Turns with the Monsters chapter. It expands upon game states by transitioning between states and implementing a turn-based game structure.
- Wandering Monsters---Introduces code to make the monsters wander randomly through the dungeon.
- Turn Based---applies state management to make the game turn based. You move, then the monsters move.
- Messages of Intent---introduces using the ECS to store messages of intent, allowing you to separate functionality between systems and use the underlying storage system as a messaging system. Makes monsters and the player use the same system to apply movement, but with different movement inputs.
Health and Melee Combat
This code covers the Health and Melee Combat chapter. It adds health to both the player and monsters, and implements a simple combat system. It also adds a heads-up display to show the current game status. It emphasizes code re-use.
- Health---adds hit points to the game and adds health display. It also introduces the concept of rendering layers, tooltips and naming entities.
- Combat---allows you to damage monsters by bumping into them, and lets them damage the player.
- Healing---introduces health restoration, initially by waiting.
Victory and Defeat
This code covers the Victory and Defeat chapter. It adds winning and losing conditions to the game.
- The Gauntlet---all about trait implementation, and using traits to make Dijkstra Maps work. Monsters path towards the player.
- Losing---detect when the player is dead, and use a game state to display a "you lost" message. Offer to play again, resetting the game state.
- Winning---adds an amulet to the map. When the player reaches the amulet, the game state changes to a "you won" screen.
Fields of View
This code covers the Fields of View chapter. It implements traits to provide functionality from bracket-lib
---specifically fields of view. It then limits monsters to chasing what they can see, and adds spatial memory.
- Field of View---introduces the concept of vision, and implements it via traits. Limits the player to displaying only what they can currently see.
- Monstrous Eyesight---adds fields of view to monsters, reusing the same code. Monsters now only chase the player when they can see it.
- Memory---keep track of where you've been with a dungeon map memory system. Show areas you've previously visited, but not areas you've yet to discover.
More Interesting Dungeons
This code covers the More Interesting Dungeons chapter. It teaches the reader to create new traits, and use them interchangeably via a trait interface. It covers various popular procedural generation techniques, providing tips and tricks along the way.
- Traits---convert map generation into a trait-based API and dynamically select algorithms at run-time.
- Traits: Rooms---convert your existing room generator to a trait-based map builder.
- Cellular Automata---implement your first real map provider, and learn to use Cellular Automata to make an interesting map.
- Drunkard's Walk---implement a second map generation technique, and learn about bounded iteration.
- Prefabs---learn to hand design your own level content, and apply it.
- Test Harness---a convenient structure for testing map builders without having to play the game over and over. Demonstrates the utility of making traits generic and then building separate harnesses in which to test them.
Map Themes
This code covers the Map Themes chapter. It extends upon reusable traits by creating a generic rendering system that can visually theme any level to the tileset of your choice.
- Themed Maps---separates the dungeon graphics into one theme, and adds a forest map theme.
Inventory and Power-Ups
This code covers the Inventory and Power-Ups chapter. It teaches you to build items with composition, manage their storage and implement item interactions.
- Potions and Scrolls---Learn to implement items, reusing a lot of existing code. Adds health potions and a magic mapping item to the game.
- Carrying Items---learn to use components to represent an item in inventory, and iterate it to display the player's inventory. Use carried items when you want to.
Deeper Dungeons
This code covers the Deeper Dungeons chapter. It extends your data storage system to provide for multi-level dungeons, with the end-game item only appearing on the final level.
- More Levels---is about making more dungeon levels, showing you how your game structure is reusable and can readily be extended into a deeper game.
Combat Systems and Loot
This code covers the Combat Systems and Loot chapter. Learn to design your entities as data---in TOML files. Learn to de-serialize the files, and use them as templates to spawn items and monsters.
- Loot Tables---introduces serialization with
Serde
, and the power it provides for quickly extending your game. Migrate your monsters to simple data files, allowing for quick iteration of new baddies. - Better Combat---further extends the data-driven aspects of the game by letting you design weapons and change monster difficulty. Change how monsters spawn, providing for a difficulty curve.