101
Announcing Rust 1.76.0 (blog.rust-lang.org)
you are viewing a single comment's thread
view the rest of the comments
[-] sugar_in_your_tea@sh.itjust.works 8 points 9 months ago

TIL about std::any.

Congrats on another release! I'll try it out this weekend. :)

[-] Ephera@lemmy.ml 10 points 9 months ago

std::any is pretty cool. You can use it, for example, to build a map where the key is just the type of the value.

So, you could query it like this:

let maybe_position = store.find::<Position>(id);

The id is the ID of an entity which may or may not have a Position associated with it.

This is similar to just using structs/OOP, so where you'd have a Vec<Entity> and then you'd call entity.position, but the big difference lies in flexibility. An Entity type would need to have all fields defined, which may ever exist on an entity.
With this type-as-key map approach, you can just tack on new attributes to entities and dynamically react to them.

All of this is basically how the storage works in the Entity-Component-System architecture (ECS), which is popular in gamedev, for example. But both the storage method and the ECS architecture are good tools to be aware of in normal software design, too.

[-] sugar_in_your_tea@sh.itjust.works 2 points 9 months ago

Yeah, I thought of runtime duck typing when I saw it, which is essentially what an ECS is.

It would be pretty cool to go the next step and be able to find and call methods or discover trait implementations on the type that may not be in the signature. So something like how Go can conditionally type asset an interface to a different interface. I don't know if that's possible in a zero cost way (probably not), but it would be interesting.

this post was submitted on 08 Feb 2024
101 points (100.0% liked)

Rust

5989 readers
22 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS