Idiomatic error handling in foor loop

Hello

I have the following code

for attr_r:Result<Attribute> in e.attributes().into_iter() {
let attr:Attribute = attr_r?;
}

This works but I wonder if there is a more idiomatic way to put the ? operator without assigning a new variable?

Well what you are doing here is what I usually do.

1 Like

You can additionally shadow the loop variable if you want:

for attr in e.attributes().into_iter() {
    let attr = attr?;
}

It might make things clearer.

2 Likes