9
submitted 9 months ago by SorteKanin@feddit.dk to c/rust@programming.dev
top 5 comments
sorted by: hot top new old
[-] SorteKanin@feddit.dk 4 points 9 months ago* (last edited 9 months ago)

Really not a fan of this approach. Makes the order of operations extremely weird. I would much prefer having super blocks that are explicitly inserted in the above scope. I.e. this:

tokio::task::spawn(async move {
    do_something_else_with(
        super { self.some_a.clone() },
        super { self.some_a.clone() },
        super { self.some_a.clone() },
    )
});

Would desugar to:

tokio::task::spawn({
    let first_block = { self.some_a.clone() };
    let second_block = { self.some_a.clone() };
    let third_block = { self.some_a.clone() };
    async move {
        do_something_else_with(
            first_block,
            second_block,
            third_block,
        )
    }
});

Only problem is if the clone is inside another block... but perhaps you could do super super { ... }? But that sort of gets crazy and makes it very hard to read again.

[-] 0t79JeIfK01RHyzo@lemmy.ml 2 points 9 months ago* (last edited 9 months ago)

I like this solution more too, what about something like?

code examples

{
    spawn(async move {
        do_something_else_with(
            ^{ self.some_a.clone() },
            ^{ self.some_a.clone() },
            ^{ self.some_a.clone() },
        )
    });
}

Which desugars to

{
    let (a, b, c) = (self.some_a.clone(), self.some_a.clone(), self.some_a.clone());
    
    spawn(async move {
        do_something_else_with(a, b, c)
    });
}

Then have like

'super1: {
    'super2: {
        spawn(async move {
            do_something_else_with(
                ^super1: { self.some_a.clone() },
                ^super1: { self.some_a.clone() },
                ^super1: { self.some_a.clone() },
            )
        });
    }
}

Which desugars to

'super1: {
    let (a, b, c) = (self.some_a.clone(), self.some_a.clone(), self.some_a.clone());

    'super2: {
        spawn(async move {
            do_something_else_with(a, b, c)
        });
    }
}

And finally, the harder to read

'super1: {
    'super2: {
        spawn(async move {
            do_something_else_with(
                ^^{ self.some_a.clone() },
                ^^{ self.some_a.clone() },
                ^^{ self.some_a.clone() },
            )
        });
    }
}

Which desugars to

'super1: {
    let (a, b, c) = (self.some_a.clone(), self.some_a.clone(), self.some_a.clone());

    'super2: {
        spawn(async move {
            do_something_else_with(a, b, c)
        });
    }
}

[-] SorteKanin@feddit.dk 2 points 9 months ago

I like the idea of using block labels, but I don't like the ^ symbol. There's already precedent for using keywords before blocks, like with async. There's also already super let in nightly I believe.

[-] Starfighter@discuss.tchncs.de 3 points 9 months ago

I was under the impression that the compiler already optimizes out a lot of clones where the clone is primarily used to satisfy the borrow checker. Is that not the case?

[-] SorteKanin@feddit.dk 3 points 9 months ago* (last edited 9 months ago)

I would say depends but generally I would think not? I mean say you clone a string because you move it in one place and also need to borrow it in another place. So you clone to avoid the borrow checker erroring out on that. As far as I know, there will be two different strings created with separate allocations and all (but I could be wrong).

this post was submitted on 11 Nov 2025
9 points (100.0% liked)

Rust

8252 readers
24 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 3 years ago
MODERATORS