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

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.