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

-1

u/VegetableBicycle686 Nov 17 '22

The lack of operations between different sizes of integer is needlessly awkward. It should be possible to add a usize to a u8 (resulting in usize) and even a u32 to a char (resulting in u32) without casts. This is something which reasonably could be fixed by writing some more Add/Sub/etc. implementations, perhaps in an edition.

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().

17

u/flickpink Nov 17 '22

God, please no. This is exactly the kind of behavior that crept into other languages and made them the mess they are today

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

7

u/globulemix Nov 17 '22

So, you mean this recently stabilized feature?

1

u/VegetableBicycle686 Nov 18 '22

Yes I'm a big fan of that feature, was pleased to see it just got stabilized.

0

u/[deleted] Nov 18 '22

[deleted] — view deleted comment

1

u/robin-m Nov 18 '22

Converting, or more precisely truncating a big types into a smaller types, or going for signed to unsigned, or from unsigned to a smaller or equal size signed is definitively a recipe for disaster. But when the result type can represent all the values of the original type (like getting an i64 from a u8), is this really a a source of bugs?

1

u/CocktailPerson Nov 19 '22 edited Nov 19 '22

It may not be a source of bugs, but it can certainly hide them. For example:

fn g(arg: i64) ->i32 {}
fn h(arg: i32) {}
fn f() -> i32 {
    let arg1 = arg_for_g();
    let arg2 = arg_for_h();
    // a
    // few
    h(arg2);
    // lines
    // of
    // code
    g(arg2)
}

This doesn't contain any unsafe conversions. But it contains a logic error that will be caught by strict typing. That's kind of the fundamental value proposition of strict typing in general: type errors are a pretty good proxy for logic errors.

There are other issues, too. Implicit type conversions could silently create optimization issues, as https://godbolt.org/z/YdM1YxeM1 shows. If you look at the generated code for each function, you'll see they're basically doing the same thing, except where add_64 has to do

    pxor    xmm0, xmm0
    pcmpgtd xmm0, xmm1
    punpckldq       xmm1, xmm0
    movdqu  xmmword ptr [rdx + 8*rax - 16], xmm1

add_i32 only does

    movdqu  xmmword ptr [rdx], xmm1

Those extra three lines before movdqu are vectorized integer promotions, and they're pretty expensive. The first one also has to maintain a loop counter in rax, where the second one can just unroll the whole loop. If type conversions were implicit, I'm not sure you'd be able to see this was happening at all. It can get even worse when you're dealing with a few layers of indirection, and in my line of work, this (sub-)optimization is definitely a bug.

1

u/robin-m Nov 19 '22

If you rely instrong typing you are not going to expose primitive type like f32 but struct Meter(f32) so I don't see how upcasting prevent logic errors.

And for the performance implications, either it doesn't matter or it will show in your profiler in your hot loop.

I consider your arguments quite weak, but this doesn't mean that you are wrong, just that I'm not conviced yet.

1

u/CocktailPerson Nov 19 '22

Eh, I'm just pointing out that type mismatches can be indicative of logic errors even if they're primitive types. And creating your own structs is fine if you control all the code you call and don't mind rewriting all the numerical traits to be able to add two Meters, but that all seems a bit silly coming from someone who's trying to avoid having to type as i64.

Implicitly coercing types and then having to come back and profile your code to figure out where it's expensive seems like a premature pessimization when the compiler can just...make you explicitly annotate the coercion.

5

u/[deleted] Nov 17 '22

Are you sure you'd want EVERYTHING to be an Option<> just in case it's out of bounds? I'd rather have it be a Result instead. But when you know what you're doing you just don't have to check which is nice, and if you do need to check, implement it. If your fn returns a Result anyway it's 1 if statement away.

2

u/78yoni78 Nov 17 '22

The index return type makes it so much more familiar to programmers from other languages

and about the operations, there are certain contexts when you don’t want to that implicit number conversion

2

u/[deleted] Nov 17 '22

I like strict typing. Makes me feel at home after Standard ML.