You are viewing a single thread.
View all comments View context
7 points
*

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

permalink
report
parent
reply
13 points
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
});
permalink
report
parent
reply
1 point

Elegant. Thanks!

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
1 point
*

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.

permalink
report
parent
reply

Rust

!rust@programming.dev

Create post

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)

Community stats

  • 572

    Monthly active users

  • 812

    Posts

  • 3.7K

    Comments