Announcing Rust 1.76.0 (blog.rust-lang.org)
from snaggen@programming.dev to rust@programming.dev on 08 Feb 2024 22:31
https://programming.dev/post/9743399

#rust

threaded - newest

sugar_in_your_tea@sh.itjust.works on 09 Feb 2024 00:15 next collapse

TIL about std::any.

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

Ephera@lemmy.ml on 09 Feb 2024 05:36 collapse

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 on 09 Feb 2024 06:29 collapse

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.

anlumo@feddit.de on 09 Feb 2024 02:36 next collapse

Oh, inspect has finally arrived! That will help a ton with debug logging.

lambda@programming.dev on 09 Feb 2024 20:33 collapse

Do you mind explaining? Maybe with the context of another languages equivalent?

owsei@programming.dev on 09 Feb 2024 21:21 next collapse

it’s just a way to use map with a reference instead of the value, by what I understood.

could be usefull for logging values in a Result so you can see it. However I think you can already do that by just mapping and returning the variable.

GissaMittJobb@lemmy.ml on 09 Feb 2024 21:43 next collapse

Looks vaguely like Stream::peek from Java, I think? There’s an equivalent method in Iterator::inspect.

anlumo@feddit.de on 10 Feb 2024 00:21 collapse

let bar: Result<T, E> = ...;
let foo = bar.inspect(|value| log::debug("{}", value));

is equivalent to

let bar: Result<T, E> = ...;
let foo = bar.map(|value| {
    log::debug("{}", value);
    value
});
lambda@programming.dev on 10 Feb 2024 00:57 next collapse

Elegant. Thanks!

xav@programming.dev on 10 Feb 2024 04:11 collapse

Warning: in the first case “value” is actually a shared reference, not a value.

bwrsandman@lemmy.ca on 09 Feb 2024 05:57 collapse

So rust finally gets reflection? In stable no less!

snaggen@programming.dev on 09 Feb 2024 06:05 next collapse

Well, if the only thing you need from reflection is the name of a type, so then yes. But I wouldn’t really call this reflection since it is very limited.

Ephera@lemmy.ml on 09 Feb 2024 06:21 collapse

Yeah, Rust can’t have proper reflection, since there’s no external runtime environment that keeps track of your state. Any such smartness either has to be compiled-in (which is how std::any and macros work) or you can implement something to keep track of this state at runtime, as if you were partially building a runtime environment.

BatmanAoD@programming.dev on 09 Feb 2024 07:02 collapse

Minor point of clarification: it can’t have runtime reflection, but in principle it could have compile time reflection.

QuaternionsRock@lemmy.world on 09 Feb 2024 21:17 next collapse

And compile-time reflection will probably also continue to suck due to some irreconcilable limitations of type-safe generic specialization. Oh how I would love an equivalent to C++ template parameter packs…

anlumo@feddit.de on 10 Feb 2024 02:55 collapse

No, the Rust Project recently made sure that Rust can’t have compile-time reflection.

bwrsandman@lemmy.ca on 10 Feb 2024 10:09 next collapse

Can you expand on this? I’d love to read more on the subject.

anlumo@feddit.de on 10 Feb 2024 12:07 collapse

Here is a short summary. The compile-time reflection project was stopped, and now nobody wants to touch that subject any more due to fear of getting the wrath of the Rust project again (the person responsible for the whole thing is still part of the leadership).

BatmanAoD@programming.dev on 10 Feb 2024 22:59 collapse

…yeah, that’s really unfortunate. Part of why I said “in principle”.

anlumo@feddit.de on 10 Feb 2024 02:55 next collapse

Unfortunately, it’s not guaranteed to be the same string all the time, so it’s rather useless for anything but debugging and logging.

PKraszewski@infosec.exchange on 10 Feb 2024 10:41 collapse

@bwrsandman @snaggen More a RTTI than reflection...

bwrsandman@lemmy.ca on 10 Feb 2024 21:58 collapse

Isn’t RTTI for polymorphic classes and stored in (or around) vtables?