all 23 comments

sorted by: hot top controversial new old
[–] 16 points 2 years ago (1 child)

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

  • source
  • hideshow 2 child comments
  • [–] 6 points 2 years ago* (3 children)

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

  • source
  • parent
  • hideshow 6 child comments
  • [–] 12 points 2 years ago (2 children)
    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
    });
    
  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 2 years ago*

    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.

  • source
  • parent
  • [–] 8 points 2 years ago (1 child)

    TIL about std::any.

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

  • source
  • hideshow 2 child comments
  • [–] 10 points 2 years ago (1 child)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • 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.

  • source
  • parent
  • [–] 1 point 2 years ago (3 children)

    So rust finally gets reflection? In stable no less!

  • source
  • hideshow 6 child comments
  • [–] [S] 16 points 2 years ago (1 child)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 9 points 2 years ago (1 child)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 13 points 2 years ago (2 children)

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

  • source
  • parent
  • hideshow 4 child comments
  • [–] 0 points 2 years ago (2 children)

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

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 2 years ago (1 child)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 2 years ago (1 child)