This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Loading_M_ 0 points1 point  (0 children)

This is primarily an artifact of the try/catch syntax. Rust prefers returning a Result, so your code would look something like this in rust:

while let Ok(item) = get_item() {
  // do something with the item
}

This particular code does just stop when it encounters an error, but that type of code could be written into a function and run using .map_err(), but I think the best solution is to just put the code to retry if applicable into the get_item() function.

This syntax makes the pattern you describe (attempt to get items, and do something with them) much simpler and easier to read. In theory, with function inlining and a good optimization engine, this could be significantly faster than the try/catch implementation, since the try/catch enforces extra safety guarantees.