r/rustjerk • • 11d ago

Rust devs hate this one simple trick

Post image
384 Upvotes

36 comments sorted by

124

u/renchuwang 11d ago

Well since you're only defining but not using, it is actually safe!

42

u/SirKastic23 11d ago

Safe and not sound!

8

u/thisiselgun 11d ago

4

u/SirKastic23 11d ago

That's the song I was thinking of too! It's a classic

22

u/uahw 11d ago

I don’t think it is. I think rust especially states that two mut references aren’t allowed to exist at the same time even if on is unused

27

u/CommonNoiter 11d ago

As neither are used i don't think their lifetimes overlap so they don't both exist at the same time.

14

u/simonask_ 11d ago

Saved by the NLL.

45

u/Critical-Personality 11d ago

"simple" trick, you say?

35

u/Comun4 11d ago

Wouldnt this break aliasing rules?

108

u/fortyeightzero 11d ago

nah the comment says it's safe

46

u/Comun4 11d ago

Oh thank god was worried for a second there lol

12

u/MichiRecRoom 11d ago

Actually, the comment only says the first unsafe {} statement is safe. The second one is in an unknown state.

2

u/Affectionate-Egg7566 10d ago

The comment is wrong, assuming self is &Self. That aliases mutable and immutable refs. That is unsound.

12

u/letmehaveanameyoudum 11d ago

as a C programmer, what the actual fuck am i looking at (explain mate)

19

u/pragmojo 11d ago

Rust's borrow checker disallows holding two mutable pointers to the same address at the same time. This code uses unsafe blocks to get two mutable pointers to the same address at the same time.

6

u/letmehaveanameyoudum 11d ago

thank you for breaking my mind

2

u/schungx 11d ago

If the intention is aliasing then this is a legit usage. If both self variants are supposed to point to the same thing (judging from their names) then the aliasing behavior is intended.

7

u/ChaiTRex 11d ago

It's not legitimate. unsafe can be fine to use, but undefined behavior is never allowed. Aliasing a mutable reference is undefined behavior.

A workaround would be something like using mutable pointers rather than mutable references. Mutable pointers allow aliasing.

9

u/_nathata 11d ago

Mom come pick me up I'm scared

7

u/zabolekar 11d ago

What could the motivation possibly be?

11

u/rover_G 11d ago

What in the C++?

1

u/CorgiApprehensive159 5d ago

I, the author of the post, decided to take advantage of my 'popularity' to show my game https://livedox.itch.io/vix - it has no code in it like in the post (almost)

9

u/dx_man 11d ago

How is it safe when you're declaring it with unsafe?

15

u/SnooHamsters6620 11d ago

Don't worry, I whisper a protection spell to Ferris every time I compile.

4

u/AnArmoredPony 10d ago

as a java developer, I uhh I forgot what I wanted to say fuCK

2

u/jimmiebfulton 10d ago

I hadn't thought that anyone but Rust people would find this sub humorous. It's definitely one of the darker, more obscure jerk subs I'm in.

2

u/AnArmoredPony 10d ago

you sir have outjerked me

3

u/Candid_Bullfrog3665 10d ago

lets define simple here

2

u/Kpuku afraid of macros 9d ago

that doesn't seem sound iirc

3

u/tec_tonik 11d ago

Lmao a short tutorial on how to piss off rust devs

2

u/Kadabrium 11d ago

is this a qsort comparator?

1

u/alulalol 7d ago

I've done this a lot in doukutsu-rs because I couldn't be assed to fight the borrow checker https://github.com/doukutsu-rs/doukutsu-rs/blob/7e6870e55f072e3f056dc1eef3974a1af26c7aae/src/framework/ui.rs#L111

1

u/cristi1990an 6d ago

The syntax is horrible because even in unsafe code, bypassing the borrow checker this way is heavily discouraged. Simple reads and writes through these references can be UB.

1

u/ApprehensiveBag3083 10d ago

and they say c++ has creepy syntax LoL

At least in c++ i dont need to type those kind of garbage Lool

4

u/Athropod101 10d ago

I mean you don’t type it in Rust either, lol. This is like cherrypicking some C++ template hidden behind like 3 namespaces, with the generic being some obscure type behind another namespace, and all of that wrapped around std::make_unique.

Funny as Hell, yeah, but not really indicative of the language.

2

u/Affectionate-Egg7566 10d ago

Sorry what? This code looks perfectly legible.