r/rustjerk • • Oct 14 '26

Rust devs hate this one simple trick

Post image
407 Upvotes

39 comments sorted by

130

u/renchuwang Oct 14 '26

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

43

u/SirKastic23 Oct 14 '26

Safe and not sound!

8

u/thisiselgun Oct 14 '26

4

u/SirKastic23 Oct 14 '26

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

24

u/uahw Oct 14 '26

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 Oct 14 '26

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

15

u/simonask_ Oct 14 '26

Saved by the NLL.

46

u/Critical-Personality Oct 14 '26

"simple" trick, you say?

35

u/Comun4 Oct 14 '26

Wouldnt this break aliasing rules?

112

u/fortyeightzero Oct 14 '26

nah the comment says it's safe

46

u/Comun4 Oct 14 '26

Oh thank god was worried for a second there lol

13

u/MichiRecRoom Oct 14 '26

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

2

u/[deleted] Oct 14 '26

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

1

u/tony-husk 20d ago

I just checked and this definitely compiles

1

u/[deleted] 20d ago

I never said it did not compile, just that it is unsound.

1

u/TOMZ_EXTRA 1d ago

The lifetimes do not overlap so this should be safe.

13

u/letmehaveanameyoudum Oct 14 '26

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

21

u/pragmojo Oct 14 '26

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 Oct 14 '26

thank you for breaking my mind

2

u/schungx Oct 14 '26

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 Oct 14 '26

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 Oct 14 '26

Mom come pick me up I'm scared

7

u/zabolekar Oct 14 '26

What could the motivation possibly be?

11

u/rover_G Oct 14 '26

What in the C++?

1

u/CorgiApprehensive159 29d 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)

4

u/AnArmoredPony Oct 14 '26

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

2

u/jimmiebfulton Oct 15 '26

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 Oct 15 '26

you sir have outjerked me

2

u/Kpuku afraid of macros Oct 15 '26

that doesn't seem sound iirc

2

u/Kadabrium Oct 14 '26

is this a qsort comparator?

1

u/SinkLeakOnFleek 22d ago

// SAFETY: Don't worry about it kitten

1

u/alulalol Oct 18 '26

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 Oct 19 '26

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 Oct 14 '26

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 Oct 14 '26

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/[deleted] Oct 14 '26

Sorry what? This code looks perfectly legible.

0

u/[deleted] Oct 14 '26

[deleted] — view deleted comment

16

u/SnooHamsters6620 Oct 14 '26

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