Chaining results

Let's say you're faced with:

tg.add_param("Name", form.name.0.clone()).unwrap();
tg.add_param("LogName", form.logname.0.clone()).unwrap();
tg.add_param("IfName", form.ifname.clone()).unwrap();
tg.add_param("RemEP", form.remep.0.to_string(MacAddressFormat::HexString))     .unwrap();

.. and you're tasked with handling these errors properly rather than just unwrap()ing, how would go you about to accomplishing this?

Note:

  • the worst case example currently has 16 such calls in succession, but that could be higher in the future; looking for a general scalable solution. (All the Result types are the same).
  • granularity isn't important (i.e. any error in this chain can be summarized as "Building parameters failed", rather than "Building Name parameter failed) -- but once one fails, there's no point in continuing.
  • The Ok case returns Ok(()).

The ? operator for the rescue.

https://doc.rust-lang.org/stable/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator

5 Likes

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.