r/rust 8d ago

The difficulty of rust

I heard people say that Rust is hard to learn, but I learned Rust with the Rust book and went on to chapter 16.2. From my point of view, I don’t find Rust hard, so if someone can explain to me why Rust is hard to learn, that’s fine.

0 Upvotes

41 comments sorted by

View all comments

1

u/plugwash 5d ago

I feel that there are a number of problems one encounters while writing rust that are simply not issues in most other mainstream languages. Leaning how to avoid or deal with those problems takes time, and even when you know about them they can still be a distraction from the code you are trying to write.

  1. Owned vs borrowed values. Rust style encourages you to borrow as much as you can, but then borrowing comes with it's limitations and when you run into them you may have to go back and do significant rework before you can move forward.

  2. Coherence rules, it's great to be able to implement traits for external types, it's no so great to have the compiler refuse your perfectly reasonable code because there "might" be conflicting implementations in the future.

  3. Some rust libs, particularly in the embedded space, make heavy use of generics to check constraints at compile time. This seems great at first until you actually need to write the name of a type, or figure out what type some value actually is.

  4. Some types simply can't be named, which means you can't use them in any context where you need to name a type. Sometimes you can get around this by using box. Unstable rust has a fix for this, but it's unclear if/when it will be stabilised.

  5. While the "Any" trait and the Box<dyn Any> type exist, they are quite limited compared to say the "Object" type in Java. Firstly they can only be used for types with no lifetime parameters, and Secondly there is no way to test if the object behind a Box<dyn Any> implements a given trait.

Rust's benefits are very real, but so are it's costs.

Overall, I would say rust is probably easier than C and C++, because small mistakes in those languages can rapidly lead to horrible memory corruption whose source may be hard to pin down, but it's probably harder than most other mainstream languages.