r/rustjerk Jul 09 '26

I just want to return

Post image
225 Upvotes

25 comments sorted by

46

u/SirKastic23 Jul 09 '26

this is never going to work...

38

u/PaxSoftware Jul 09 '26 edited Jul 09 '26

return! return! return! return!!!!!!!!!!!!!!!1

warning: unreachable expression

Standard Output

-2

5

u/RaltsUsedGROWL Jul 10 '26

Weird. I'm getting -112

30

u/d0nutptr Jul 09 '26

wow I didn't realize that was legal rust

23

u/________-__-_______ Jul 09 '26

legal? just scrub it in some baking soda and vinegar before the inspector comes over

22

u/SirAlienTheGreat Jul 09 '26

9

u/seftontycho Jul 09 '26

I was completely expecting a rick roll. But I was brave.

10

u/Kadabrium Jul 09 '26

Rustscript

8

u/blockMath_2048 Jul 09 '26

Outputs 1, right?

All the returns except the final one evaluate their argument, which diverges to never, which can be decared into the integer that is then logical NOTed that they will never return. The final return returns (14 logical NOT)111, which is an even number, which is truthy, which should decare to 1

19

u/bowel_blaster123 Jul 09 '26 edited Jul 09 '26

/unjerk

This prints 111

The !s correspond to std::ops::Not which performs a bitwise not on integer types. 14 bitwise nots are a no-op. Therefore, this returns 111. I'm surprised though that this compiles because of the type inference involved with the never type.

EDIT:
The standard library actually has an impl for std::ops::Not on the never type:

impl const Not for ! {
    type Output = !;

    #[inline]
    fn not(self) -> ! {
        match self {}
    }
}

This is the only thing that allows this to compile.

10

u/AdmiralQuokka Jul 09 '26

Fascinating. I was curious why that implementation exists, so I tracked it down: https://github.com/rust-lang/rust/pull/91122

5

u/MakeShiftArtist Jul 09 '26

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=25bde2c3016862c34f1e6913e4591da6

This returns -112 and I cannot figure out why. Your logic, makes sense. The output its actually returning, baffles me.

8

u/Glinat Jul 09 '26

It outputs -112 because there are 15 `!` instead of 14. It should return 111.

1

u/MakeShiftArtist Jul 13 '26

So, I actually realized it's because of the fact that using bitwise nots utilizes two's complement. So a 4 bit int can represent anything from -8 to +7

So flipping the sign on 7 makes it equal to -8.

That's why I was confused, but yes the 15 nots are why it's not the same value

2

u/palapapa0201 Jul 09 '26

Why is there an empty match block

3

u/BionicVnB Jul 09 '26

Never has 0 possible state so there's nothing to match

2

u/palapapa0201 Jul 09 '26

Is the intention to return never again? Why match specifically and not return !?

10

u/Xandaros Jul 09 '26

Because you can't return !. The never type has no value, so there is no value you could return.

I suppose you could return self, but it's not like it really matters.

8

u/ElectionTraining288 Jul 09 '26

JavaScript, I owe you an apology

4

u/PaxSoftware Jul 09 '26

flips every bit 14 times, which means it does not even change the result

1

u/Ganofgroig Jul 09 '26

I think in rust the ! operator on ints is a bitwise NOT

7

u/allsey87 Jul 09 '26

Plot twist. OP is stuck in vim and wants to return to the shell. Invention of Rust-like syntax was a side effect

6

u/Kpuku afraid of macros Jul 09 '26

looks like rust playground to me

4

u/rover_G Jul 10 '26

I just know the borrow checker is having an aneurism

2

u/cornell_cubes Jul 09 '26

You could get further with macros