this post was submitted on 24 Sep 2023
210 points (93.0% liked)

Programmer Humor

32031 readers
1352 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[โ€“] DWin@sh.itjust.works 2 points 11 months ago* (last edited 11 months ago) (1 children)

It feels like maybe this could be a code structure issue, but within your example what about something like this?

fn main(){
    let mut counter = 0;
    let output_array = array.into_iter()
        .map(|single_item| {
            // breaks the map if the array when trying to access an item past 5
            if single_item > 5 {
                break;
            }
        })
        .collect()
        .map(|single_item| {
            // increment a variable outside of this scope that's mutable that can be changed by the previous run
            counter += 1;
            single_item.function(counter);
        })
        .collect();
}

Does that kinda syntax work for your workflow? Maybe it'll require you to either pollute a single map (or similar) with a bunch of checks that you can use to trigger a break though.

Most of the time I've been able to find ways to re-write them in this syntax, but I also think that rusts borrowing system although fantastic for confidence in your code makes refactoring an absolute nightmare so often it's too much of a hassle to rewrite my code with a better syntax.

[โ€“] thejevans@lemmy.ml 2 points 11 months ago

Thanks for this! I'll see if I can work something like this in.