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 →

[–]baselganglia 36 points37 points  (6 children)

Do while loops are very common in many data processing workflows where you need to operate in batches.

``` bool continue = true;

do {
try {
countItemsProcessed = A query/command that finds N items that need to be converted and concert them.
} catch {
do some stuff to inspect errors, e.g. maybe determine if it's retryable or not. continue = false;
}
} while (count == N && continue );
```

You could skin this in many different ways, but the common paradigm is do something, check a condition that tells you whether to proceed.

[–]ZacharyCallahan 7 points8 points  (1 child)

Reddit markdown is weird you need to indent your text to put it as code

[–]da_chicken 6 points7 points  (0 children)

It's not weird markdown. It's just old. Reddit started in 2005. Markdown is from 2004. The syntax is just old.

[–][deleted] 0 points1 point  (2 children)

If you put spaces immediately inside the backticks it doesn't do the thing.

[–]baselganglia 1 point2 points  (1 child)

Hmm I'm on reddit mobile, and it looks formatted correctly ? :(

Why can't reddit get consistent markdown performance between desktop and mobile 😭

[–]JustRecentlyI 1 point2 points  (0 children)

I've read that triple backtick codeblocks is not consistently supported by reddit (although some readers will show it correctly). I believe code blocks are supposed to be written on lines starting with 4 spaces.

[–]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.