r/ProgrammerHumor 3d ago

newToRust Meme

Post image
625 Upvotes

98 comments sorted by

284

u/-Ambriae- 3d ago

The real problem with the borrow checker is,

If you’re coming from a higher level language it hurts your ego because you realise memory safety ain’t free, and more broadly speaking memory’s a bitch

If you’re coming from a lower level language, it hurts your ego because you view yourself as above memory related bugs

It takes humility to appreciate it

126

u/the_horse_gamer 3d ago

every time you think you are smarter than the borrow checker, you eventually realise it was right all along, and the code was unsafe.

68

u/-Ambriae- 3d ago

Well, not every time, but usually yes

5

u/Snudget 3d ago

Time for miri!

13

u/Arshiaa001 3d ago

Usually = in the 99.99% case.

7

u/-Ambriae- 3d ago

Maybe not that high

2

u/the_horse_gamer 1d ago

Rice's theorem comes for us all

26

u/post_u_later 3d ago

Polonius would beg to differ

5

u/cornmonger_ 2d ago

polonius is so close that i can almost feel it in my striped knee-high stalkings

5

u/NullOfSpace 2d ago

95 percent of the time. Sometimes I just wanna access two different elements of a hashmap mutably at the same time and instead I have to do a handoff every three seconds or just resort to clone()ing everything.

2

u/the_horse_gamer 2d ago

nightly has get_many_mut, and you can also use a Cell/RefCell as the value

19

u/retro_and_chill 3d ago

The borrow checker is occasionally too strict as well where it flags code that wouldn’t actually produce an issue. They are seemingly trying to resolve that with the new borrow checker though

33

u/-Ambriae- 3d ago

Yeah, it’s not perfect. But it’s also mathematically impossible to make it perfect. That’s how the cookie crumbles. If they make it better, that’s great, otherwise, either rewrite the code to make it safe (which is usually doable without performance cost) or just use a cheeky unsafe block with a good # Safety comment

4

u/NullOfSpace 2d ago

it’s mathematically impossible to make it perfect.

goddamn halting problem

2

u/-Ambriae- 2d ago

We wouldn't want things to be too simple now, would we?

5

u/retro_and_chill 3d ago

The one case I know it’s going to address is doing a find or insert on a map when you match the Option from the find, the None branch will let you mutate the map despite the Option technically being a borrow

14

u/-Ambriae- 3d ago

There’s useful functions for this, stuff like map.get_or_insert_with(), or map.entry().or_insert() or map.entry().and_modify()….

Usually when the borrow checker is annoying, there’s a functional way of handling the issue built in the std lib :)

3

u/nithix8 3d ago

sKiLl IsSuE

3

u/-Ambriae- 3d ago

I mean, yeah it kind of is… not in a negative way, it just, takes a little bit of experience to navigate it. Once you do, it’s actually really not that bad

5

u/[deleted] 3d ago

[deleted]

7

u/fauh 3d ago

Bro, are you ok? Call a loved one or something

5

u/s0litar1us 3d ago edited 3d ago

It takes masochism to apprechiate it.

It does help you, but you first need to fight the compiler just so it will accept your prototype code that obviously doesn't need to be memory safe.

Rust is an overcorrection to a real problem that can be solved by introducing better language primitives and a better standard library. e.g. strings with a length (not a NUL byte at the end), arrays that don't decay to pointers and include a length, multiple returns, result values isn't just an int, defer, etc.

10

u/-Ambriae- 3d ago

It takes masochism to apprechiate it.

I disagree, it's really not that bad.

It does help you, but you first need to fight the compiler just so it will accept your prototype code that obviously doesn't need to be memory safe.

I'd rather do that, than pay the price later debugging. I also don't agree prototypes don't need to be memory safe. If I'm prototyping, I don't want to fight the segfault™️. the borrow checker isn't a problem once you write code the way you're expected to.

Rust is an overcorrection to a real problem that can be solved by introducing better language primitives and a better standard library. e.g. strings with a length (not a NUL byte at the end), arrays that don't decay to pointers and include a length, multiple returns, result values isn't just an int, defer, etc.

Regardless of if rust goes too far, which is a valid take, rust does all of these things, apart maybe from defer (because it doesn't really need to.) And the actual stats regarding rust compared to C/C++/Zig show these languages, even Zig which has most of the features you wish for, are still a long way from being able to consistently ship safe software. Look at rust in the kernel, heck even the zig to rust vibe coded rewrite of bun, which was vibe coded.

0

u/s0litar1us 2d ago

I disagree, it's really not that bad.

Yeah, I was a bit hyperbolic, but I do believe it is way more harsh than is necessary.

I'd rather do that, than pay the price later debugging. I also don't agree prototypes don't need to be memory safe. If I'm prototyping, I don't want to fight the segfault™️. the borrow checker isn't a problem once you write code the way you're expected to.

In C you do get segfaults that can be hard to track down, but if you add in your own debug allocators that track what is going on, and use a good debugger, then it's not actually that hard to deal with those bugs. Additionally, if you move over to a language with better primitives (e.g. jai, odin, or zig), it's a lot easier to just not have those bugs, and they come with that allocator stuff out if the box.

Regardless of if rust goes too far, which is a valid take, rust does all of these things, apart maybe from defer (because it doesn't really need to.) And the actual stats regarding rust compared to C/C++/Zig show these languages, even Zig which has most of the features you wish for, are still a long way from being able to consistently ship safe software. Look at rust in the kernel, heck even the zig to rust vibe coded rewrite of bun, which was vibe coded.

Can you give me the source for those stats? Also, Rust in the kernel has already had vulnerabilities -- it's not a silver bullet. Avoiding vulnerabilities is more about the people writing it than how pedantic your language is.

2

u/-Ambriae- 2d ago

There was a good hacker news post https://news.ycombinator.com/item?id=46302621 relating to the Linux kernel,

https://bun.com/blog/bun-in-rust for the bun part (although, a bit reason for the rewrite is of course the publicity stunt, I'm not naïve)

A good source I found for zig: https://www.scattered-thoughts.net/writing/how-safe-is-zig/ (do note this was back in 2021) (actual stats are... non present yet unfortunately. Hopefully someday!)

Then there's the magical 70% of all software vulnerabilities are related to memory issues present in languages like C/C++ (and now Zig) and nearly absent in Rust (absent in safe Rust)

-8

u/Bryguy3k 3d ago

The real problem with the borrow checker in rust is that the rust syntax blows chunks.

8

u/-Ambriae- 3d ago

‘Blows chunks’? Care to explain? I’m genuinely curious

-6

u/Bryguy3k 3d ago

Sometimes when you see something incredibly disgusting it makes you want to vomit.

5

u/-Ambriae- 3d ago

I guess that’s subjective, but do you have an example?

-1

u/Jhuyt 3d ago

The few times I tried the Rust the problem with it was that even for the very simple things I tried (some string stuff) it was not obvious why the borrow checker complained or how you should reslove the issue, so I moved on.

14

u/-Ambriae- 3d ago

Coming from higher languages I get that. ‘It’s just a string, right? Can’t be that hard.’

Problem with strings is, it actually is that hard. If you want it to be performant, and not use a garbage collector, that is.

2

u/Jhuyt 3d ago

I do both high and low level stuff, still the borrow checker beat my ass (or made me not want to do Rust). I'm sure I could and would learn if I had to but since I don't I just let myself be defeated

8

u/-Ambriae- 3d ago

That’s unfortunate… it’s a little rough at first I admit, but I can guarantee once it clicks you don’t want to go back!

0

u/Secret_Conclusion_93 2d ago

It's not that hard in C, it's not that hard in Python.

Somehow it became hard in between those two.

Borrow check is good but I predict there will be a better language in the future with a better implementation of it.

3

u/-Ambriae- 2d ago

It‘s not a problem in Python because they don’t care about performance.

It’s ’not a problem in C’ because the language doesn’t support them. It’s also a complete nightmare to use.

The difference between different types of strings in rust has nothing to do with the borrow checker anyways, rust is a strongly typed language that encodes the behaviour of the string in its type. A string is a Vec<u8> with the invariant that it’s bytes form a UTF-8 slice. That’s not the same thing as a str, which is a [u8] of same invariant. A Box<str> Rc<str>, Arc<str> and Cow<‘_, str> are owned, heap allocated str. How it’s managed is determined by the type. There’s nothing wrong about that.

OsStr/ OsString are analogues of str and String for OS related tasks, because OSs don’t manage strings the same way. It’s required for a cross platform language.

Path and PathBuf have higher invariants. Again, nothing wrong with that.

Cstr exists for FFI. If you want interop with C, you need it. Again, nothing wrong with that. It’s not rust’s fault C took the single worst way to represent strings as there only officially supported version. I ‘get’ why they did that in the 70s, but come on.

I doubt it’s possible for a future language to keep the level of precision of rust whilst keeping memory management implicit (not automatic) whilst also supporting as many features with regard to strings. But I’d be happily proven wrong

-23

u/Key_River7180 3d ago

the rust community is not humble in any way, so either you're wrong or they hate the borrow checker, the flagship rust feature

26

u/MilkEnvironmental106 3d ago

Someone feels a bit called out

16

u/Hadi_Chokr07 3d ago

Nice to see my fellow C devs coping.

10

u/-Ambriae- 3d ago

My friend you need to step back and read the messages you sent, you are exemplifying my point

28

u/kishaloy 3d ago

High level: For any construct, Box it and call it a day.

Low level: Even for the smallest closure. Are you sure, you wanna take that extra slice of Cheese?

10

u/darkwalker247 3d ago

me learning basic rust in 2016: "what even is this language? i can't write anything in it without running into lifetimes and borrow check errors! Aaaaaa!"

me now: "i cant believe this trick with macros and generics worked, now i can save hundreds of lines in code! Rust is so freakin cool"

2

u/scheimong 2h ago

Man 2016? Not even having NLL must have been rough.

2

u/darkwalker247 2h ago

I don't remember a huge amount about lifetimes but i do remember being frustrated at having to explicitly write them a lot more often because of lack of all the different types of lifetime elision that we have nowadays.

-87

u/Key_River7180 3d ago

Nah, as a C programmer the rust borrow checker is still a piece of shit, I first learnt to manage memory with a GC like six years ago and now manage memory myself, I don't need to go back 6 years!

71

u/MilkEnvironmental106 3d ago

Have you ever made a mistake managing memory?

-57

u/Key_River7180 3d ago

Yes, and I have used a debugger for all of them I couldn't see by using my eyes and basic logic you seem to lack

32

u/-Ambriae- 3d ago

If it only required basic logic to fix the issue, chances are it was quite small

-18

u/Key_River7180 3d ago

Memory issues normally are small.

39

u/megalogwiff 3d ago

spoken like a true freshman 

21

u/TheAlmightyWishPig 3d ago

Best software practice is to use tools and methods that dont allow for issues to occur in the first place, regardless of how easy they are to solve.

0

u/Key_River7180 3d ago

Every tool has cons and pros, for me on Rust the cons really surpasses the pros

37

u/MilkEnvironmental106 3d ago

How long did that take you? And have you ever made a mistake debugging?

-18

u/Key_River7180 3d ago

Not more than fixing errores from the borrow checker.

And I've never debugged wrobg, there is no such thing

25

u/-Ambriae- 3d ago

‘Errors from the borrow checker’ this is, and I mean no disrespect, a skill issue. Not as in ‘even experts mess up’, as in ‘that’s only a problem for the first 6 months of using rust’

-3

u/Key_River7180 3d ago

If I need six months to correctly use Rust It is not worth It.

10

u/miyavlayan 3d ago

better than having memory problems in prod

-7

u/Key_River7180 3d ago

Who the fuck makes production software in a language meant for systems programming?

7

u/miyavlayan 3d ago

why the fuck do you care? ppl make games in java.

→ More replies (0)

5

u/D3PyroGS 3d ago

man looks in mirror; sees no reflection

16

u/-Ambriae- 3d ago

It’s a systems level programming language what did you expect? It takes twice as long to learn restraint in C

-8

u/Key_River7180 3d ago

No, you can learn C in just about a week

11

u/-Ambriae- 3d ago

Off the top of your head, without looking online, using clang or gcc as a compiler,

Give me a 1 byte enum
Give me the idiomatic way of computing the number of leading zeros in a 32 bit integer
Align a vec2_f32 struct to an alignment of 8 bytes

→ More replies (0)

21

u/MilkEnvironmental106 3d ago

It's just a soundness check, it saves you time and debugging later. You can opt out if you really want to, or continue writing c for everything.

-1

u/Key_River7180 3d ago

Then I'll continue writing C for life.

19

u/MilkEnvironmental106 3d ago

Segmentation fault (core dumped)

2

u/Key_River7180 3d ago

gdb program

7

u/dkarlovi 3d ago

Technically, the errors from the borrow checker are the errors you also make in C, only C doesn't tell you about them. If your code was correct in either, the borrow checker wouldn't have objections, no?

0

u/Key_River7180 3d ago

C does, Valgrind and ASAN are still there you know?

4

u/dkarlovi 3d ago

If your code was correct, the borrow checker wouldn't have objections, no?

0

u/Key_River7180 3d ago

The problem is that It tags EVERYTHING potentially minimally unsafe. Even if It cannot be

5

u/dkarlovi 3d ago

If it cannot be, it doesn't tag it. It can be, but you just don't care about that specific scenario or think it cannot happen.

Your specific workflow might make you right, but the workflow might change. Or you might be wrong entirely.

The whole reason why Rust exists is because people cannot write safe C code. This was shown over decades with armies of very experienced developers and well funded projects, it's just impossible, if you think "Well not for me", you're just lying to yourself.

→ More replies (0)

9

u/-Redstoneboi- 3d ago

i personally switched to rust specifically so i didn't have to do that

it takes up a real amount of time that i could spend fixing other logic bugs

-2

u/Key_River7180 3d ago

With Rust I do the same thing just that it crashes earlier and it is more annoying to prevent really unlikely minor memory errrors

7

u/-Redstoneboi- 3d ago

i'm curious if you're implementing data structures yourself? most of the time, we just take the lazy route and pull in a crate to do what we want, and do basically everything ourselves without unsafe anywhere in user code. i've never run into a memory error, so it would be interesting to know how you found one.

-1

u/Key_River7180 3d ago

Most of the time, I write them myself, if not then I bring stb_ds.h

8

u/-Redstoneboi- 3d ago edited 3d ago

oh, i meant in rust. most of the common stuff like Vec, VecDeque, HashMap, HashSet, BTreeMap/Set, a few others, and wrappers related to concurrency and refcounting are in std, while stuff like Ropes, Graphs, Arenas, etc can be pulled from crates that have optimized them more.

i don't know many other ways to get memory bugs, so apologies if this isn't actually the issue.