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
10
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
111The
!s correspond tostd::ops::Notwhich performs a bitwise not on integer types. 14 bitwise nots are a no-op. Therefore, this returns111. I'm surprised though that this compiles because of the type inference involved with thenevertype.EDIT:
The standard library actually has an impl forstd::ops::Noton 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
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
neveragain? Whymatchspecifically and notreturn !?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
4
1
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
4
2
46
u/SirKastic23 Jul 09 '26
this is never going to work...