515
you are viewing a single comment's thread
view the rest of the comments
[-] 30p87@feddit.org -5 points 3 months ago

Average Rust code:

macro_rules! sum {
    ( $initial:expr $(, $expr:expr )* $(,)? ) => {
        $initial $(+ $expr)*
    }
}

fn remove_prefix<'a>(mut original: &'a str, prefix: &str) -> &'a str

let mut up = 1;
    'outer: loop {

Hell I don't want to know what you define as ugly then.

[-] asdfasdfasdf@lemmy.world 25 points 3 months ago* (last edited 3 months ago)
  1. Macro syntax technically isn't even Rust
  2. This is definitely not average Rust code.
[-] tatterdemalion@programming.dev 1 points 3 months ago

Sorry, I love Rust but I can't really agree with you here. They only showed a macro_rules! definition, which is definitely rust syntax. Lifetime annotations are relatively common.

I will concede that loop labels are incredibly rare though.

[-] fruitcantfly@programming.dev 4 points 3 months ago

Loop labels are rare, but they lead to much simpler/clearer code when you need them. Consider how you would implement this kind of loop in a language without loop variables:

'outer: while (...) {
    'inner: while (...) {
        if (...) {
            // this breaks out of the outer loop, not just the inner loop
            break 'outer;
        }
    }

    // some code here
}

In C/C++ you'd need to do something like

bool condition = false;
while (...) {
    while (...) {
        if (...) {
            condition = true;
            break;
        }
    }
    if (condition) {
        break;
    }

    // some code here
}

Personally, I wouldn't call it ugly, either, but that's mostly a matter of taste

[-] Ephera@lemmy.ml 0 points 3 months ago

Well, you'd typically put the loops into a function and then do an explicit return to jump out of there. I believe, there's some use-cases where this isn't possible, which is why I'm cool with loop labels existing, but I've been coding Rust for seven years and have not needed them once...

load more comments (2 replies)
load more comments (4 replies)
load more comments (7 replies)
this post was submitted on 17 Dec 2025
515 points (95.9% liked)

Programmer Humor

30761 readers
1289 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS