Idiomatic error handling in foor loop


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.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.