Rust is a nightmare to learn coming from Java

& * 'a &'a Box || macro! mod crate extern crate #[...] String vs &str &self unwrap expect ? Rc Arc

I was so happy and excited with my first println!("hello") program but more and more things get complex till a point i turn crazy

Guys, how are you able to understand such a complex programming lang ? You are all engineers ??

6 Likes

Yeah, Rust can be pretty sigil and jargon heavy at times.You learn to get used to it, though, and it becomes second nature. Take it slow, learn a concept at a time. One of the big parts that I like about Rust is that most of the pieces are self contained - you don't need to know anything about what an Rc is to know about macros, so you don't usually need to learn a bunch of things at the same time to start making programs. Except for lifetimes, but those are so fundamental there's not really any other way to do it.

(I will admit, coming from Haskell was a benefit to understanding. So many infix operators I still don't remember the meaning of.)

11 Likes

I come from Java too but also know C. Yeah, Rust is challenging but it's well worth it. There are bits that I still don't get, but it doesn't stop me from writing program that I'm reasonably pleased with. Once you know Rust a little bit (I've been at it for one year) you'll be amazed how good your programs are (even comparing to Java that I've been writing professionally for years).

6 Likes

Yes, that is true. While both may look vaguely similar due to curly braces and a String type, they are very different languages.

Rust isn't object-oriented (beyond some syntax sugar) and doesn't have garbage collection. This does heavily affect how programs need to be structured, and what programmer needs to pay attention to. Coming from Java you'd probably want inheritance, getters and setters, and model parent-child relationships, and these things are not natural in Rust.

Rust isn't too bad. Once you learn how to think in Rust, it's a productive language. All these *, &'a and Boxes have a very good reason to exist, and they enable writing very fast, multi-threaded programs without NullPointerException.

If switching to Rust feels like jumping into deep end, you could try Kotlin first. It shares some of the higher-level concepts with Rust like non-nullable types and more functional approach, but it's still on JVM, so you don't have to micro-manage strings vs string views.

35 Likes

Thank you guys. Very friendly community here :slightly_smiling_face:

I'll keep on working knowing the journey will not be easy :grimacing:

12 Likes

I am an engineer, yes. But I can empathize with your feedback. Coming from a language like Java or C# is going to leave you with a bit of culture shock with Rust. It's even worse when your primary language is JavaScript or Python!

FWIW, I learned programming the hard way with the C language. I've been bitten so many times by undefined behavior and incomprehensible Heisenbugs that Rust was easily sold to me when I actually looked into the language goals. That's a very different perspective from the other languages mentioned; As long as you have a ton of memory, you're probably not going to have too many complaints with the language.

That's not to say the transition from C-thinking to Rust-thinking was easy! Not by any stretch. Actually it took me a good long several months to become comfortable with Rust's unique qualities (particularly the need for lifetime annotations).

My first step was reading The Book, literally cover-to-cover, and most importantly following along with Chapter 2; not just reading! It's important to experience compiler errors with a guide to walk you through it. Because you will get a lot of compiler errors on your journey. The sooner you realize the compiler is your friend (not your enemy) the more you will be thankful for every error.

17 Likes

As a Java developer playing with Rust during my spare time, debuts have been a little bit (ok very) difficult but the main issue was quite often the same: trying to write in Rust what I would have written with Java.

Like @kornel said, Rust isn't an OOP language so you need to reason differently...and by far more carefully, considering aspects you may have forgotten because of the JVM doing the job for you.

The compiler is great at helping you (ok, first you hate it, then you love it) and most of the times, once your code compiles, it does what you expect it to. There are many documentation and books available, the community is welcoming and patient with noob like us :rofl: + there are lots of crates (serde, slog, failure, once_cell,...) that really helps once you get used to it.

The result is really worth a try (performance and resource consumption are just on another level) and it may even change the way you reason about your Java writing, for the better I think.
When it comes to performance tuning or issues, Java is not always that easy to get it right. You'll just end trying to figure out what happens and deal with an OS over another OS ('cause that's what the JVM truly is).
If performance is a matter to you, the real difference to me is that in Rust, you face it from the very beginning.

Rust is challenging, for sure, but once you have it right, it's more a road to a new world than a nightmare.

PS: If you don't know it, https://readrust.net/ is a daily set of information, crates, tools and articles about Rust and it's really worth having a look at it from time to time.

10 Likes

One bit of advice that got me up and running was to avoid putting references into structs. That decision let me come to understand why the borrow checker was complaining without getting bogged down trying simultaneously to figure out lifetimes.

The other thing that helped was paying attention to the compiler error messages. In most languages they seem to be designed to obfuscate, so I learned to ignore what they say. Not in Rust. Often the error message will tell you exactly how to fix the problem.

One other bit of advice, that I don't know if it is recommended, is to be liberal in your use of cloning to get around borrow check errors. That let me get a sense of accomplishment when my program finally ran. Early on, clone() was scattered all over my code like salt on french fries. As I came to understand Rust better I was able to get rid of most of them.

26 Likes

Languages like Rust, C, and C++ have different objectives than languages that take away most of your abilities to control memory usage like C#, Java, and Javascript. In these GC'd languages, there is very little use of the stack and practically everything you create goes on the heap with little control over where that ends up in memory. In addition to the nasty impact of a GC, you have to work hard in these languages to put your data in contiguous memory if you are concerned about performance. That means you are using the hardware pretty ineffectively. If the problem you are solving has CPU to spare and the speed to execute a task or cost of hardware is not a concern, you can use these "easier" languages and solve your issue at a high level without thinking about memory. But if you are trying to max out your performance, you need to think about your memory usage and layout. And Rust really shines with its borrow checker checking every one of your memory model assumptions. I think if you focus on how memory is laid out when your code is running, the L1, L2, L3 processor caches, and the likely cost of running your code, you will start to appreciate what Rust is doing for you.

1 Like

This worked for me too!

6 Likes

Wow ! So many constructive answers on this amazing forum. Thank you guys, readrust.net is added into my favorites :sunglasses:

2 Likes

Don't focus too much on all the low-level details immediately. If you have never ever before thought about how memory is allocated, how a compiler generates closures, how objects are laid out in memory, you aren't going to understand all of this at once.

So maybe start with some higher-level aspects. Take some of the syntax for granted while learning, don't be preoccupied with all the things you need to write β€” come back to them and understand them fully later.

Or maybe try writing a fair bit of C and C++ first? Then come back to Rust – it will be much easier to see why it has the rules it has.

9 Likes

BTW, while we are at strong complaints:

Rust is a nightmare to learn coming from Java

I'd say this is also true the other way around. :stuck_out_tongue:

12 Likes

coming from Java

Do you know what a nightmare to write Java for someone who is used to C++ and Rust?
So spend some time with Rust and then you gonna dislike Java

2 Likes

I recommend working through Introduction - Rust By Example -- not just reading it, but manually typing in every example.

At the end of it, although I didn't understand Rust "design patterns", I became quite familiar with Rust syntax + how to fix common typos.

3 Likes

Hey there,
may be you could also consider, when coming from Java, to take a look at Scala fist. It introduces all the functional programming style and takes you away from OO programming thinking while still working mainly with a Java Syntax as it finally transpires to Java Code. From Scala than the step to Rust (which I personally favor over Scala) does not feel that hard then :slight_smile:

So from my point of view itβ€˜s not the language or syntax that makes the transition hard, it’s the different kind of concept and the need to re-think how to write algorithms and code differently in a non-OO framework.

3 Likes

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