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

154

u/mina86ng Nov 17 '22
  • Rust is move-heavy which is not something compilers were optimised for. This results in some unoptimised code. This is fixable by improving the compilers.
  • Lack of specialisation. This is fixable without introducing breaking changes.
  • std::ops is a mess when trying to work with generic numeric types. Writing code in a way where you don’t relay on the type being Copy or without doing unnecessary clones is unreasonably verbose. I don’t know if this can be fixed without a breaking change.
  • Unsafeophobia by which I mean that some programmers are zealously avoiding unsafe even if it can be shown that the code is safe and noticeably improves performance. Can this be fixed? Maybe if Rust gets wider spread into areas where people care about performance more.

36

u/Sw429 Nov 18 '22

Unsafeophobia by which I mean that some programmers are zealously avoiding unsafe even if it can be shown that the code is safe and noticeably improves performance. Can this be fixed? Maybe if Rust gets wider spread into areas where people care about performance more.

100% this. I've had people criticize my own libraries for using unsafe code in a few places, despite the fact that I clearly document exactly why and how what I am doing is sound. For some reason, some people don't understand that unsafe does not necessarily mean unsound.

26

u/Nilstrieb Nov 18 '22

Unsafe code is okay sometimes and I agree that people are against it too much, but it must be said that a lot of unsafe code out there is buggy, so the fear does not come from nowhere. If you can reasonably avoid unsafe, you should.

5

u/[deleted] Nov 18 '22

It depends on the balance. For example, a 3D game needs to squeeze out every last drop of performance. Using unsafe largely improves runtime performance during unwrapping (If you've already unwrapped the value successfully), using std::mem for lower-level memory control etc. A web server should be avoiding these practices, of course.

I try to avoid using unsafe as much as possible in Rust, but coming from a C background, I've got used to dealing with memory bugs so it also matters whether you're ready to actually risk crashing something due to segfaults.

16

u/zesterer Nov 18 '22

With respect, we've almost never felt the need to reach for unsafe when writing r/Veloren, despite the game having pretty intense performance requirements nowadays.

The only occurrences of unsafe we have are related to the plugin API, something that needs to be unsafe because the compiler doesn't have the ability to understand the necessary invariants.

The idea that "unsafe = performance" is a myth. It's much more useful when building up abstractions or getting around limits in the compiler's ability to reason about invariants.

3

u/[deleted] Nov 18 '22

Doesn't mean that not using unsafe will cause your code to be slower than Python. What I meant is that unsafe can be an extra boost if you absolutely need it.

→ More replies (1)

5

u/Nilstrieb Nov 18 '22

Your unwrapping example can often be written in a better safe way that's just as fast.

But yes, sometimes unsafe is necessary. But you'll have to make sure that it's correct, run it through Miri if possible, also test it with ASAN and document it accordingly and then it's okay.

→ More replies (4)

3

u/quick_dudley Nov 18 '22

Most of the times I've used unsafe blocks it's just been to call C code. But my second most frequent use so far is getting mutable references to more than one element in a Vec at the same time.

4

u/[deleted] Nov 18 '22

Most of the times I've used unsafe blocks it's just been to call C code

The way I go about this is declare the functions and wrap them nicely in Rust afterwards. I believe that's what gtk-rs does too.

3

u/robin-m Nov 18 '22

getting mutable references to more than one element in a Vec at the same time

If you still have a mutable reference to the whole Vec, then your code is absolutely UB.

Btw, do you know about split_at_mut? But I admit that if you try to access to more than one elements in the array with at least one of them mutable, it’s annoying to do

→ More replies (3)

3

u/[deleted] Nov 18 '22

despite the fact that I clearly document exactly why and how what I am doing is sound.

Why you think it is sound. Part of the issue is that the rules around unsafe Rust code are still not really known, and also it's really hard to know if unsafe Rust code is sound. Even harder than C.

So I think it's reasonable to avoid it as much as possible.

32

u/SV-97 Nov 17 '22

std::ops is a mess when trying to work with generic numeric types. Writing code in a way where you don’t relay on the type being Copy or without doing unnecessary clones is unreasonably verbose. I don’t know if this can be fixed without a breaking change.

imo it's not really std::ops in particular. Generic numerics in Rust are just generally... painful and frustrating. And num doesn't exactly help here.

And given that a lot of core functionality is still implemented in C / Fortran you often times gotta resort to just using f32 / f64 even if the complete rest of your code is perfectly capable of working with "arbitrary" numeric types.

→ More replies (2)

3

u/Jomy10 Nov 18 '22

The last one is definitely true. Some people really dislike unsafe no matter what

18

u/[deleted] Nov 17 '22

Not trying to start a fight; but if I’m using unsafe rust blocks because they’ve been tested, why wouldn’t I just use 40ish years of tested C or C++ code?

85

u/fiedzia Nov 17 '22

Because tiny amount of unsafe is better than 100%, better error messages, saner language, better tooling. Also a chance to find other people willing to work with you (and agreeing on a language features/tooling).

27

u/rusty_macroford Nov 17 '22

Personally, I don't see any reason to rewrite perfectly good C or C++ code in rust, but I would prefer writing new code rust over C++ for several reasons:

  1. templates can't be independently typechecked
  2. macro_rules is way more powerful than #define
  3. the borrow checker prevents use after free as long as your unsafe code base actually works.

I wouldn't underestimate the third point, because you can do a lot with a small number of tiny unsafe blocks.

3

u/zesterer Nov 18 '22

perfectly good C or C++ code

See, this is the problem.

How do you evaluate whether code is 'perfectly good'?

The C compiler isn't going to warn you if it's not 'perfectly good'. The only thing you've got to go on is whether it appears to be working fine, today, on the current hardware you're using, with the specific inputs you're giving it.

Rust gives us the tools to make stronger promises about whether software is 'good' based on criteria enforced by the compiler. Most of these criteria still apply in unsafe blocks (although they aren't transitive due to the nature of UB).

5

u/rusty_macroford Nov 18 '22

I don't think you are right. Let's take Linux as a specific example of a C code base. It's been around for 30 years, and for almost as long, people have been running elaborate whole program static checkers against it. Among other things (such as deadlocks) these checkers detect exactly the kind of errors that rust's borrow type system does. These checkers don't promise to detect all errors, but rust's promise to detect all errors is only as good as the trusted code inside rustc. If rustc contains more than 0 bugs, it doesn't provide an absolute guarantee either.

So, if a Unix kernel where written entirely in safe rust, would I trust it not to panic on a SEGV more than I trust Linux. Yes, I would have a tiny bit more trust. But would I trust the rust OS to correctly implement POSIX and all the relevant BSD and SysV APIs that go into Linux? Not until another 30 years have gone by, and even then only if the hypothetical kernel gets a user base comparable to Linux's.

I feel like you may be severely underestimating how easy it is to tell whether safe rust code is "perfectly good."

22

u/kukiric Nov 17 '22 edited Nov 17 '22

It's easier to maintain soundness in a Rust project with unsafe sprinkled in some locations than a C/C++ project which is unsafe everywhere.

The ecosystem is also largely built on safe code, so you're much less likely to get a raw pointer that needs to be babysitted from a Rust library than a C/C++ one.

33

u/mina86ng Nov 17 '22

Because you in Rust you limit and explicitly mark where unsafe code goes.

But yes, Rewrite in Rust is another cultural weakness of Rust. There are times where using 40ish years old C or C++ code is better than trying to rewrite it in Rust.

4

u/VegetableBicycle686 Nov 17 '22

Because you still get the benefits of lifetimes and the borrow checker for everything other than the unsafe operations, even within the unsafe blocks.

4

u/nacaclanga Nov 17 '22

Because you keep the unsafe to the parts where it is really needed and explicitly marking the issues. If you have a code that was successfully employed for 10 years, with virtually no changes and small external API, things might look different, but if you are constantly updating it, changes are high, that some future upgrade might ignore some unspecified invariant and mess things up.

2

u/Sw429 Nov 18 '22

Unsafe code means that the potential for UB is confined to the unsafe code blocks. No one is using unsafe code in every line if their Rust. This is different from C++, where every line could result in UB.

→ More replies (3)

6

u/[deleted] Nov 17 '22

Unsafeophobia is probably the number one problem.

4

u/VanaTallinn Nov 17 '22

Unless it's rewriteophilia.

→ More replies (4)
→ More replies (15)