Rust is a nightmare to learn coming from Java

What?

Someone want's to learn Rust and the advice is to learn C/C++/Scala first?

Something does not sound quite right about that to my mind.

But if you must, I suggest learning and practicing some assembler first. Then you get a appreciation of what that memory thing is all about. :slight_smile:

6 Likes

Well without any other context given this sounds weird, I would agree, however it was given that someone is coming from a different language and learning a different language syntax + new programming model might be tough, so learning the new concepts and models of rust staying in a language with a similar syntax might help overcoming the first burden. Once the concepts are understood and you get to use to the concepts it might be easier to switch then to the target - Rust in this case. So you might not need to learn a different language in its entirety but similar already known syntax can help in a smooth transition :wink:

1 Like

Yes, you heard that right.

If one has never dealt with details of how natively-compiled code works under the hood, and is used to everything being a magic implicit reference type with a runtime always (supposedly) doing the right thing, then they'll have a hard time peeling off all the nice abstractions Rust provides over these gory details.

I've been teaching programming to beginners for several years now, and the invariable pattern I have observed is: those who started with C or C++ will learn, for example, scripting languages easily, while the converse is not true. Trying to dive straight into Rust might be intimidating because one doesn't only need to learn about all the low-level stuff that is happening, but one also needs to keep in mind how to express them at the high level Rust is offering.

Thus, being able to write a program with malloc() and free() by hand, and seeing for yourself how they are always paired in a structured manner, may well be a valuable experience for appreciating RAII, for instance.

10 Likes

This is also why I don't understand the "learn scala first" advice: the things a beginner is most likely to hit their head against is I think not FP (which can be learned equally well in Rust I think) but things like allocation, borrows and ownership, lifetimes, macros, advanced trait usage.

I don't pull these examples out of thin air, these were IIRC the results of a past Rust survey about what people had the most trouble with when learning Rust.

Honestly I think Scala's syntax soup will only confuse matters... for a beginner.

6 Likes

Agree, take a quick look at C if you have the time to invest on low level matters like allocating, handling and freeing memory explicitly (malloc, free and pointer arithmetic).

Scala is an Object & Functional language (not my words, theirs) and Rust is none of that so I don't really understand how it could help. Rust is imperative with some FP pattern, like Java tends to be since Java 8 (less than Rust but in progress).

The Rust book (https://doc.rust-lang.org/book/) is kind of exhaustive, and if you can get the "Programming Rust" (O'reilly) book, even though a little old now, it offers great explanations on how Rust handles memory and concurrency (it miss the async part that came later but it's really not a problem).
This article is a "quick" remainder you can use: https://fasterthanli.me/blog/2020/a-half-hour-to-learn-rust/. It's not a deep dive into Rust internals, but it enumerates a lot of standard features and concepts you'll have to deal with.

'hope you'll enjoy your investment in Rust :grin:

Yep, a lot of good advice here. :slight_smile:

What I always come down to when Rust gets difficult is that it is paying off for me big time with:

  • Catching all kinds of bugs at compile-time instead of making me run the program before running into them. This saves hours upon hours upon hours of debugging time.

  • Running blazing fast for my users and making my apps easy to install.

Yes, there are no doubt the epic struggles that will ensue between you and the compiler and your brain as you try to figure out why you had to &**my_var or whatever it is, but your application will serve your users with speed and efficiency in a way that you would not be able to provide without Rust.

Edit: Oh and I think the first rust Koan should be near mandatory reading for anybody who has ever run into a Rust compiler error ( which is everybody that writes Rust ). :slight_smile:

7 Likes

Not Scala, but I would say that someone who knows both C++ well-ish and Haskell well will have an easy time with Rust. If you've mastered Haskell then advanced trait usage is something you're familiar with, seeing as traits are basically type classes with some different syntactic conventions (+ some different orphan rules & and some complicated rules re. object safety). Typed FP languages typically also have strong pattern matching facilities. Moreover, Haskell prepares you for "symbolic / equational thinking", which is helpful for understanding lifetimes (although the subtyping is unfamiliar).

5 Likes

The benefits of learning Rust greatly outweigh the hurdles at the beginning, and in the end will make it all worth the effort.

6 Likes

I know the challenge, but from a different perspective. I come from a mixed programming background that is mainly rooted in COBOL.
I agree with a lot of the other commenters, though - it is worth the challenge! I especially enjoy getting back to the system-level thinking and relearning some of the fundamentals that I have not had to deal with in a long time.

1 Like

Glad to see i am not the only one :sweat_smile: . I didn't expect my topic to be so hot :yum:

5 Likes

I feel like I'm starting to sound like a broken record in plugging it again and again but, once you've got some practice with building applications using .clone() and other people's crates, Learn Rust With Entirely Too Many Linked Lists is great help in wrapping your mind around ownership to a level where you can start to write data structures without getting frustrated.

6 Likes

It's not my experience. It went quite smooth. Only the lifetime annotations can sometimes drive me mad. But I expected it to be much harder to learn then it was. Worked through the book at first.

2 Likes

Don't sell yourself short based on titles you may not have. Rust only seems complex because it's foreign to you. Things should click one by one, over time. Lifetimes are pretty complex if you've never had to think too much about memory before, but it takes learning and practice to master them, not titles or degrees.

22 Likes

I think this is very true. I remember learning Java and being completely confused by class, public, static, interface, extends, implements, etc. Any programming language is confusing if you aren't familiar with the concepts it uses. But you learned Java even though it was confusing in the beginning, so you can learn Rust.

6 Likes

"It's not painful to learn something if you learn it incrementally."

-- Yo-Yo Ma at his NPR tiny desk concert. :smile:

7 Likes

I think it has been said already, but I just want to emphasize it again: learning Rust coming from a language which did memory management for you will require some study and will cause some frustration. I think trying to get into C from Java will do the same, just less obvious, since broken code will compile, and you probably won't realize that that thing you're doing all the time causes serious trouble until you move away from toy examples to learn the language. So the feeling is that you "get the hang of it" quicker, but that's not true. Rust just forces you to learn those things up front and prevents you to take the "quick and easy" route, with the payoff that when you get your code to compile, it will probably work.

8 Likes

I had this exact experience in college. I took a summer course after my first year where we used C. I didn't learn about memory management. So when I implemented merge sort and started making variable sized arrays, I had no idea why I was getting a whole lot of garbage. I eventually learned about malloc after hours of banging my head against a wall. It worked! Jump forward 7 years. I started learning Rust from the rust book and I realized that I never called free on that memory.

6 Likes

These are both excellent beginner tips. I see way too many people get stuck in lifetime soup by just adding more and more all over, when really they just wanted to store a String. One can always use more references -- especially in things like function arguments -- as one gets more comfortable.

11 Likes

No problem you will get it only try make projects with rust. I'm javascript developer and now I can start making some projects with this amazing language.

On top of all the great advice above (avoid storing borrows, liberal use of clone at first, etc.), I will give a seemingly small suggestion: use nightly (but not features) and update often. There are many stumbling blocks that have been ironed out in the compiler that will make your learning experience much better. We have diagnostics improvements landing every other day, sometimes quite significant ones at that! It would be a shame if you got stuck on things that won't be as big of a problem 12 weeks down the line when they hit stable.

Finally, be aware that some concepts just need some time to "bake" in your head. The first time I tried to learn rust I had to put it down for 6 months before I could actually do something useful with it. But I feel it is worth it.

14 Likes