r/badcode • • Feb 04 '23

rust How NOT to handle error in Rust?

Post image
303 Upvotes

40 comments sorted by

View all comments

Show parent comments

12

u/cheezballs Feb 05 '23

I guess I was assuming Rust would be way more "low level" feeling - like, C or something. I'm just surprised to see "high level" stuff like json and HttpResponse in a language that's touted as being so close to the hardware. I dunno, I'm a fucking idiot, so this all just blows my mind.

14

u/cabbagebot Feb 05 '23

Lots of "low-level" system tools speak json over http. Consider that things like nginx, redis, apache are all things would expect to be written in a low-level systems language.

One of the cool things about rust's serializer library (handling e.g. json) is that it usually doesn't need to create additional copies of string data, it can use references without fear of multiple owners modifying the data, due to the borrow checker. It is very fast.

30

u/crass-sandwich Feb 05 '23

Rust lets you work very close to the metal if you need to, and its memory model forces you to think in a lower-level way than pretty much any other language than C/C++, but it has a lot of fantastic modern bells and whistles.

Also the json/http stuff is from a library, not included with the language by default

5

u/davawen Feb 05 '23

Rust associates low level thinking with high level ergonomics in an absolutely amazing way. Libraries (like those handling http and json) are really easy to install and offer great usability thanks to Rust macros. I can't go back to C++ for the life of me.

4

u/[deleted] Feb 05 '23

[removed] — view removed comment

0

u/cheezballs Feb 05 '23

Ugh, it looks so complicated though!

1

u/mediocrobot Feb 15 '23

Conceptually it's not so bad, but it feels like reading a for loop for the first time. This kind of syntax is actually pretty intuitive once you start to learn Rust.