• anlumo@feddit.de
    link
    fedilink
    English
    arrow-up
    19
    arrow-down
    2
    ·
    11 months ago

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

    • λλλ
      link
      fedilink
      arrow-up
      7
      ·
      edit-2
      11 months ago

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

      • anlumo@feddit.de
        link
        fedilink
        English
        arrow-up
        15
        arrow-down
        2
        ·
        11 months ago
        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
        });
        
        • xav
          link
          fedilink
          arrow-up
          1
          ·
          11 months ago

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

      • GissaMittJobb@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        11 months ago

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

      • owsei
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        11 months 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.