r/rust • • Nov 17 '22

What are Rust’s biggest weaknesses?

What would you say are Rust’s biggest weaknesses right now? And are they things that can be fixed in future versions do you think or is it something that could only be fixed by introducing a breaking change? Let’s say if you could create a Rust 2.0 and therefore not worry about backwards compatibility what would you do different.

220 Upvotes

390 comments sorted by

View all comments

Show parent comment

23

u/csreid Nov 17 '22

Also the decision to make the Index operation (square brackets) generally panic if out of bounds makes it essentially useless for any application where panics are unacceptable. a[i] should return None if out-of-bounds, instead of requiring a separate method, get().

Yikes, big no from me. Maybe we're just in different worlds (probably), but handling Nones all over the place sounds awful and anyway, if I'm indexing something out of bounds, my code is probably broken and I'd wanna know about it.

If anything, it could maybe return like an IndexOutOfBounds error type or something, but that still makes the code pretty messy.

18

u/[deleted] Nov 17 '22

I!d argue that having a panicking index and a get that returns an option was the right call.

2

u/VegetableBicycle686 Nov 18 '22

> different worlds

I think that's it actually yes. Panics have very different severities in different applications and different platforms, and might result in a nice user-facing error message in a GUI/web app, or crash the whole application if panic=abort. So there's perhaps no way to please everyone.

I find it quite interesting that there's been strong support in this thread for the existing panicking Index behaviour when the casual use of `unwrap()` is frowned upon though.

3

u/LadulianIsle Nov 18 '22

Generally because they imply:

unwrap = lazy

panic! = I have no idea how to deal with this

[] = I know what I'm doing

I think the closest equivalent to panic! is actually expect

1

u/MrTheFoolish Nov 18 '22

IMO the language designers chose the correct indexing default. It's both familiar for those coming from other mainstream langs and also safe. There's only so much unfamiliarity budget one should spend if mainstream adoption is desirable, and lifetime+ownership already spends a lot of that budget. Rust indexing satisfies all practical use cases.

  • index operator: safe, panics
  • get: safe, returns Option
  • get_unchecked: unsafe, guarantee no bounds checks are generated