r/rust • • Sep 24 '24

Rust, Autism, and Correct Code

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

150 comments sorted by

View all comments

Show parent comment

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

6

u/shponglespore Sep 24 '24

The convention in Go is to return an error value that is nil on success, not a boolean error flag. It's clunky but at least somewhat sane.

2

u/glasket_ Sep 25 '24

Except for when you retrieve a value from a map, assert a type, or read from a channel; then you get a (T, bool) just because.

2

u/shponglespore Sep 25 '24

I'd say in those situations, the absence of a value isn't an error, so there are no details to be reported using an error value. Those are the same kind of situations where you'd use Option rather than Result in Rust, or Maybe rather than Either in Haskell, etc.

2

u/syklemil Sep 25 '24

But that just maps to a regular old nullable T in Go, doesn't it, not (T, bool)? If all you need is to represent a fallible get that sometimes has nothing, there's no need for the second value. And if there is a need to return some error information, doing it with a boolean is pretty crap.

It kind of comes off as if they're trying to approximate an Option<Option<T>>. I guess the usecase is something like "you got a nil here, but that's actually what the value is, don't quit whatever you were doing"?

2

u/shponglespore Sep 25 '24

I guess the usecase is something like "you got a nil here, but that's actually what the value is, don't quit whatever you were doing"?

Bingo. That exact scenario came up in a coding interview I did earlier this week.

3

u/zekkious Sep 24 '24

You say, the (bool, MaybeValidT)? Or the Optional[ValidT]?

5

u/proudHaskeller Sep 24 '24

(bool, MaybeValidT)

2

u/met0xff Dec 07 '24

Also the Python linters complain if you just use an Optional without checking.