Rust Entity Component Systems: ECS Libraries for Rust Game Dev
Tags: rust, gamedev
Published: 2024-02-07 18:22:24

---
title: "Rust Entity Component Systems: ECS Libraries for Rust Game Dev"
published: "true"
description: "Rust Entity Component Systems 🧩 a look at ECS choices for Rust game development, including Bevy ECS, Specs and 🦀 Rust bindings for Flecs."
tags: "rust, gamedev"
canonical_url: "https://rodneylab.com/rust-entity-component-systems/"
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbf5q8sfs2z5ea9bpg13.png"

---
## 🦀 Rust Entity Component Systems

Here, we will have a look at some Rust Entity Component System (**ECS**) libraries, and also, resources for finding out more about ECS generally and building your own ECS.  I am new to Rust game dev and documenting what I learn as I progress.

In a recent post, I went through <a href="https://roneylab.com/rust-for-gaming/">some options for Rust game development engines</a>.  In that post, we looked at Godot and Bevy, which are both build around ECSs.  We look at the **Bevy ECS** later, here, as you can use it with other game engines, independently of Bevy.  Most of the other engines considered, though, did not have an ECS built in, so we look at some alternatives here.

## 🧩 What is an ECS?
Before looking at whether you need an Entity Component System for your game, let&rsquo;s first see what an ECS is!

### Components

Entity component systems provide structures and operations for managing game elements.  The three main elements are entities, components, and systems.  We&rsquo;ll look at components first.  **Components**, in an ECS, hold data on some actor (or game object).  The actor might be a player or non-player character (NPC), or even some inanimate object, like a door.  

You might want each of those actors to be able to interact with the game world in different ways.  Both the player and the door might have a collision box represented as data components, used to help us update game state when the player goes through the door.  The player and NPC might have velocity components holding data on their current speed and direction, so we can move them by the right amount, over each iteration of the game loop.

### Entities

Entities are data too; they are collections of logically associated component instances.  A player entity might have position, velocity, and collision components.  Then, the door might just need position and collision components.  From the ECS perspective, an entity is essentially an ID or handle on a game object, linking its components.

### Systems

Systems can be thought of as functions that manipulate component instance data to update them.  As an example, a **movement system** could take current position and velocity data to update actor positions.  That system can loop through all entities that have a velocity, and position component, but ignore any entities that do not have both of these.

### Resources and Schedulers

It doesn&rsquo;t end with entities, components, and systems!  Typically, we will have a **game world**, **resources** (for managing global properties, such as frame timing) and **schedulers** too.  You might use schedulers, to help ensure that systems run in a certain, logical order, or after some, particular, change in the game state.

### Resources for Learning more about ECS
That is just a quick introduction to ECS, and if you are new to them, you will probably want to explore some of these resources to learn more:

- <a href="https://specs.amethyst.rs/docs/tutorials/01_intro">The Specs Book</a> takes you through adding an ECS to your game, and includes a good introduction to ECS in general.
- <a href="https://www.youtube.com/watch?v=aKLntZcp27M">Using Rust For Game Development</a> keynote speech by Catherine West with a longer look at how the ECS approach benefits versus an object-oriented approach, and also some **alternatives to an ECS** for a smaller game.
- <a href="https://github.com/SanderMertens/ecs-faq/blob/master/README.md">ECS FAQ</a> deep dive into ECS from the creator of the C/C++ flecs ECS. 

## 😕 Why use an ECS?
The main reason for opting for an ECS approach over object-oriented is probably code maintainability.  The ECS philosophy is **composition over inheritance**, and the approach makes it easier to add extra components later, efficiently.  The ECS approach can also save on code duplication for similar objects.  Finally, there can be runtime performance benefits, for games with large numbers of entities and types, working in some languages.

## 🧱 Building your own Rust ECS
Entity Component Systems can be performance critical elements of your game, and they are not easy to write, so why try?  In the previous, Rust game engine post, I mentioned that you probably don&rsquo;t want to build a game and a game engine!  Similarly, if you are building a game, you probably don&rsquo;t either want to build an ECS for it.  That said, you might want to run through a quick tutorial on building an ECS, just to get a better understanding of  how they work.

You might also want a basic ECS for your small game, rather than integrating one of the libraries mentioned below.

In either case, see the <a href="https://ianjk.com/ecs-in-rust/">Writing a tiny Entity Component System in Rust tutorial</a>, by Ian Kettlewell to get going!

## Rust Entity Component System Libraries
Here, we look at game development ECSs written in Rust.  In this section:
  <ol>
  <li><a href="#rust-ecs-bevy-ecs">Bevy ECS</a>;</li>
  <li><a href="#rust-ecs-hecs">hecs</a>;</li>
  <li><a href="#rust-ecs-shipyard">Shipyard</a>; and</li>
  <li><a href="#rust-ecs-specs">Specs</a>;</li>
  </ol>

### Rust ECS: Bevy ECS
**Performance and ergonomics focussed ECS**

Bevy is probably the most popular Rust game development engine. It takes a data-driven approach, centred on the ECS.  As mentioned, earlier, you can use Bevy&rsquo;s ECS with other game engines.  Bevy ECS aims to be ergonomic, using normal Rust data types, and no need or complex lifetimes, traits or builder patterns.  It has Entities, Components, Worlds, Resources, Schedules, and Systems.

**Learn more about Bevy here**:

- Rust crate: <a href="https://crates.io/crates/bevy_ecs">bevy_ecs</a>
- <a href="https://bevyengine.org/learn/book/getting-started/ecs/">Bevy ECS section of the Bevy book</a>
- <a href="https://github.com/bevyengine/bevy/blob/main/examples/ecs/ecs_guide.rs">Bevy ECS Guide</a> 
- <a href="https://saveriomiroddi.github.io/learn_bevy_ecs_by_ripping_off/">Learn Bevy&rsquo;s ECS by ripping off someone else&rsquo;s project</a> book on building a Bevy ECS based on the Hands on Rust game, which used Legion.
- GitHub repo: <a href="https://github.com/bevyengine/bevy">bevyengine/bevy</a>


### Rust ECS: hecs

**Minimalist Rust ECS**

hecs is a minimalist ECS, considered more a library than a framework.  

**Learn more about hecs here**:
- Rust crate: <a href="https://crates.io/crates/hecs">hecs</a>
- <a href="https://github.com/Ralith/hecs/tree/master/examples">hecs code examples</a>
- GitHub repo: <a href="https://github.com/Ralith/hecs">Ralith/hecs</a>

### Rust ECS: Shipyard

**Rust ECS for highly parallel processes**

Shipyard focusses on usability and speed.  Shipyard&rsquo;s data structure is inspired by the C++ **EnTT ECS** library&rsquo;s core data-structure. There is a book and <a href="https://leudz.github.io/shipyard/square_eater">fun, little online game</a> (links to book and game code below) to help you get going quickly.

**Learn more about Shipyard here**:
- Rust crate: <a href="https://crates.io/crates/shipyard">shipyard</a>
- <a href="https://leudz.github.io/shipyard/guide/master/">Shipyard Users&rsquo; Guide</a>
- <a href="https://github.com/leudz/shipyard/tree/8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49/square_eater">Square Eater game written using Shipyard ECS and Macroquad</a>
- GitHub repo: <a href="https://github.com/leudz/shipyard">leudz/shipyard</a>

### Rust ECS: Specs

**Early Rust ECS, still popular**

Specs has a classic ECS design and was responsible for popularizing the ECS pattern in Rust.

- Rust crate: <a href="https://crates.io/crates/specs">specs</a>
- <a href="https://specs.amethyst.rs/docs/tutorials/01_intro">The Shipyard Book</a>
- <a href="https://github.com/amethyst/specs/tree/708c3ac436e9cad285babd61caf54fa15a0edace/examples">Specs code examples</a>
- <a href="https://csherratt.github.io/blog/posts/specs-and-legion/">Detailed look at Specs and Legion</a>.
- GitHub repo: <a href="https://github.com/leudz/shipyard">leudz/shipyard</a>

### Rust ECS: Legion

Legion was intended as a replacement for specs in the amethyst game engine, though neither amethyst nor legion are currently maintained.  See the link to the detailed look at Specs and Legion above for more on Legion.

## Rust Bindings for C++ ECSs: Flecs

There are <a href="https://github.com/jazzay/flecs-rs">Rust bindings for the popular C++ ECS flecs</a>.

## 🙌🏽 Rust Entity Component Systems: Wrapping Up

In this Rust Entity Component Systems post, we got an introduction to ECS and, more specifically, Rust ECS.  In particular, we saw:

- an **overview of the ECS pattern**;
- some **ECS learning resources**; and
- four **actively maintained Rust ECS libraries**.

I would love to hear from you, if you are also new to Rust game development.  Do you have alternative resources you found useful? Is there something missing here that you would have added.  Equally, if you are a veteran of the ecosystem and have some pointers, I would also like to hear from you.  Let me know if you decide not to go down the ECS route, or even if you plan to build your own.

## 🙏🏽 Rust Entity Component Systems: Feedback

If you have found this post useful, see links below for further related content on this site.  Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please <a aria-label="Support Rodney Lab via Buy me a Coffee" href="https://roneylab.com/giving/">consider supporting me through Buy me a Coffee</a>.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via <a href="https://twitter.com/messages/compose?recipient_id=1323579817258831875">@askRodney</Twittea> on X (previously Twitter) and also, join the <a href="https://matrix.to/#/%23rodney:matrix.org">#rodney</a> Element Matrix room. Also, see <a aria-label="Get in touch with Rodney Lab" href="https://roneylab.com/contact/">further ways to get in touch with Rodney Lab</a>. I post regularly on <a href="https://roneylab.com/tags/gaming/">Game Dev</a> as well as <a href="https://roneylab.com/tags/rust/">Rust</a> and <a href="https://roneylab.com/tags/c++/">C++</a> (among other topics). Also, <a aria-label="Subscribe to the   Rodney Lab newsletter" href="https://newsletter.rodneylab.com/issue/latest-issue">subscribe to the newsletter to keep up-to-date</a> with our latest projects.