r/rust • • Sep 24 '24

Rust, Autism, and Correct Code

https://youtu.be/MZdxbf0_fPg?si=fdThISrw6ubN1mXK
186 Upvotes

150 comments sorted by

View all comments

357

u/MichiRecRoom Sep 24 '24 edited Sep 24 '24

One of the comments on that video sums it up best for me:

I like Rust for the same reason why I like math. There are no blurry maybes, only strict yes or no. It feels good to not need to track needless complexity everywhere, only having to consider a task at hand, trusting compiler to do its job.

68

u/daniels0xff Sep 24 '24

Yes! Exactly how I feel. After I finish writing some code I feel I know exactly how it will work and how it will behave when being executed. For some reason I have total confidence in that code. When I write JS/PY I either spend a lot of time doing super defensive coding trying to cover as much as I can think of or i still worry that sometimes things might not work as expected.

31

u/MichiRecRoom Sep 24 '24

And importantly: Even if your Rust code doesn't behave as you expect, the changes needed to make it do so are often minimal. For example, I've occasionally written > when I meant <. Or sometimes, I've assumed a number would come out one way, but I need to add another function call to make it actually come out that way.

But I've never had to throw away all of my code.

7

u/[deleted] Sep 24 '24

After I finish writing some code I feel I know exactly how it will work

So, can we use this as a reason to get out of writing test cases? 👀

21

u/-Redstoneboi- Sep 24 '24

No. You can use this as a reason not to test your test cases.

15

u/vplatt Sep 24 '24

Also as a reason to not need to write test cases to cover cases that your type system didn't.

1

u/MichiRecRoom Sep 24 '24

Can I use it as a reason not to test my test case test cases?

0

u/mr_birkenblatt Sep 24 '24

Try TS or PY with types

26

u/daniels0xff Sep 24 '24

I do. You still have exceptions, I hate try/catching everything. Code never seems to be properly documented if something can/will throw or not.

21

u/Vakz Sep 24 '24

Rust: come for the memory safety, stay for the error handling

Rust has made me hate working with exceptions in other languages

12

u/mbecks Sep 24 '24

Pattern matching and nested enum data are up there too!

8

u/zekkious Sep 24 '24

I feel this as well! In python, people at my job wrote a library with some fallible functions.

Instead of returning T | None = Optional[T], it returns (bool, T), where bool is an indicator of whether it was ok, but it won't catch all errors, and T might return as empty, like in pd.DataFrame() with no data.

Having an Optional[T] would be so much simpler, clearer and easier to handle!

4

u/proudHaskeller Sep 24 '24

Btw, that's also Go's preferred method of error handling

→ More replies (7)
→ More replies (1)

21

u/JamsPae Sep 24 '24 edited Sep 24 '24

Writing Rust reminds me of that one older TheOnion video about the autistic reporter interviewing a prisoner, getting increasingly ecstatic as he is told about the rigid structure and schedules of prison life.

Yes, Rust is the prison in this analogy, but it's one we all like and understand. Contributing to libraries and comprehending foreign codebases is (imo) way easier in Rust as opposed to C++ where many larger projects bend the language to its will. This can be desirable for some, but i don't fucking think so >:(

11

u/MichiRecRoom Sep 24 '24 edited Sep 24 '24

Contributing to libraries and comprehending foreign codebases is (imo) way easier in Rust as opposed to C++ where many larger projects bend the language to its will.

Sadly true. About the only C++ codebase I feel confident comprehending, is Godot Engine. Seriously, go look at the code for your favorite node - there's a good chance that if you remade that node in GDScript, the code would look nearly identical (bar the syntax differences).

3

u/temasictfic Sep 25 '24

Yes, that's actually true when you consider some of the stuff like Cells, Guards, etc.

1

u/plugwash Sep 25 '24

Yes, Rust is the prison in this analogy,

Not much of a prison when you have the key.

1

u/JamsPae Sep 25 '24

I'll acknowledge the analogy is a strange one, but it's mostly in reference to its conventions and that there is a stricter sense of what's "idiomatic" and that we kindof like it that way.

0

u/[deleted] Sep 24 '24

[deleted] — view deleted comment

1

u/jamincan Sep 24 '24

The no_panic crate might help you out.

2

u/[deleted] Sep 24 '24

._. I can relate since I’m a mathematics major but I study a lot of computer science as well as currently I’m doing symmetric key management research and Rust has been one of my favorite languages but I’ll state that I’m somewhat biased since much the computer I’m reading and analyzing is rust code.

1

u/chuckdoe Sep 24 '24

This is the way.

1

u/global-gauge-field Sep 25 '24

For those of you who read geometry, topology and physics by Nakahara, it is very similar to reading application of abstract math (e.g. topology, abstract algebra) to physics, it can be very complex at times, but I enjoying reading and understanding it. It is describe complex topics in a way that is simple enough, but not too simple.