Is Rust supposed to be that hard?
from JohnHammerSky@lemmy.today to programming@programming.dev on 11 Jun 07:10
https://lemmy.today/post/54584368

I decided to adventure myself in Tauri development for a personal project, I read the entire Rust official book and followed the exercises. When I first started developing it was like if nothing I learned helped for real life projects.

Now after getting betting up every single time I touch my project, it seems I’m catching things slowly.

But I’ve never seen such a hard modern language, I used C and C++ before and it’s incomparable.

#programming

threaded - newest

slacktoid@lemmy.ml on 11 Jun 07:19 next collapse

Its more of what do you want out of a compiler.

manning.com/…/learn-rust-in-a-month-of-lunches

A great analogy for Rust is that of a critical but helpful spouse.

And I kinda agree with that. Maybe a little excessive but when you’re done you have a much more likely to not hit a segfault

one_old_coder@piefed.social on 11 Jun 07:32 next collapse

How many years of experience do you have in C++, and which version?

Rust can be a bitch in its syntax, and its borrow checker, but modern professional C++ can be way worse if you use concepts and metaprogramming.

TheTechnician27@lemmy.world on 11 Jun 07:40 next collapse

I’d also add that the borrow checker, to me, has a grossly overexaggerated difficulty/annoyance. It follows a simple set of a few easily learned rules, and in my experience, if you break one, it’ll tell you which and where. I feel like the type of C/C++ programmers complaining about it are mostly the ones that have mountains of hidden memory etc. bugs in their C/C++ code that Rust actually makes them clean up.


Edit: Another class I find are those who kind of just feel out the borrow checker blindly without sitting down for 20 minutes to learn how ownership works.

FishFace@piefed.social on 11 Jun 10:20 collapse

It depends what you’re trying to do. Some data structures inherently do not work comfortably with a single-mutable-ownership model, and while they’re not exactly ubiquitous, they’re common enough. (My exposure to rust is through advent of code where they’re more common than in the real world).

Rust doesn’t make it impossible, but you need to convert everything to Rc Refcell and there’s a load of annoying crusty boilerplate, so it is more difficult. And yes, the guards in those calls can prevent or expose nasty errors, but a lot of the time - in a simple app you’re writing to learn the language - the logic that keeps everything safe is so simple that an experienced programmer doesn’t even think about it, and then it’s confusing because it’s not clear what you’re being protected from :)

calcopiritus@lemmy.world on 11 Jun 14:58 collapse

Do you really need that much Rc? That is, do you really need multiple ownership for a piece of data in a single thread? It is rarely the case, many times you can get away by just borrowing that data.

ARc is harder to avoid, since across threads you often really need the multiple ownership.

Next is, do you need RefCell? Or would a simple Cell in some of the struct fields be enough?

JohnHammerSky@lemmy.today on 11 Jun 07:46 collapse

No I’m not professional, maybe I’m mistaken. I just know C++ and made a few simple things, and then I tried to do a few simple things in Rust but it’s almost killing me. I’m asking myself if it’s worth it.

30p87@feddit.org on 11 Jun 07:49 next collapse

If your code touches sensitive stuff (eg. public networking) and needs to be low level, probably Rust (or another compiled memory safe language). Otherwise, just use C++.

JohnHammerSky@lemmy.today on 11 Jun 07:54 collapse

But then what’s the point of Tauri? I mean there are plenty general use projects in Tauri, why’d they chose Rust?

30p87@feddit.org on 11 Jun 08:05 next collapse

A fake sense of security by pretending that simple apps that don’t expose low level interfaces and use wrapping libraries for all parts of networking need to be implemented by-design memory language, or for people who just like rust.

fruitcantfly@programming.dev on 11 Jun 08:42 next collapse

Once you’ve learned it, Rust is just a very nice compiled language to work with.

You get higher level constructs than in C++, a language without a billion weird edge cases, a modern package manager, and much more. In my experience, my code written in Rust is more likely to work as intended, both because of the stricter compile-time checks, but also because language features like sum types make it easier to check the core logic at compile time.

I work in both C++ and Rust, among other languages, but these days I never reach for C++ for a new project

dontbelievethis@sh.itjust.works on 11 Jun 11:38 next collapse

Tauri is for using webtech with rust irc.

So if you want to use rust in combination with JS frameworks like reakt you use tauri.

calcopiritus@lemmy.world on 11 Jun 15:04 collapse

Do you really need tauri?

Tauri is for web devs that want to make GUIs with web tech in rust. You can do GUIs without web tech.

If you really want to make a GUI with rust, you can use iced.

If you just want a GUI with web tech, do it in JavaScript+html.

If you want a GUI without web tech and don’t care the language, use a GUI toolkit for your preferred language.

Learning a GUI toolkit is hard. Learning a language is hard. Learning both at the same time is even harder than the sum.

Mikina@programming.dev on 11 Jun 07:54 next collapse

I think that is kind of the main point of Rust, though.

It’s pretty easy to make something in C++. But it will very probably have a lot of hidden issues with memory, undefined behaviors and the like. Rust doesn’t let you make those mistakes that much, and forces you to do it correctly and securely the first time, which is why it is harder to get into.

They are mostly harmless and may never cause problems for you, but that’s how you get critical RCEs that are 8 years old in a software that’s now widely used.

If you don’t need this kind “ease traded for security”, in my personal opinion I’d go with Zig instead.

one_old_coder@piefed.social on 11 Jun 08:13 collapse

It’s worth it because it’s not C++. If I could, I would get a job writing Rust. Or Zig as that other guy said. My shitty opinion:

  • Zig <-> C
  • Rust <-> C++
dwt@feddit.org on 11 Jun 07:37 next collapse

Yes. Hard that from Lots of people.

Kwdg@discuss.tchncs.de on 11 Jun 07:41 next collapse

Rust has a steep learning curve early on. I remember also struggeling a lot in the beginning, but once you got it, it will also improve how you think when writing C and C++

TehPers@beehaw.org on 11 Jun 07:45 next collapse

What do you find hard about it?

For me, what made it take so long to learn and really understand was that it’s different from most modern programming languages. It’s not C, C++, or based on my own experiences, C#, JS, Java, etc. Approaching the language as someone who’s really into C# made it difficult to throw away that experience to learn something completely new, whether because I now had to wrap my head around lifetimes or because I can’t have one type inherit the fields and methods of another.

Eventually, if you keep sticking to it (and have interest to do so), you’ll learn how the language was designed to be used, and why it was designed that way.

Reading source code is your friend, by the way. If you want to learn the language, you should spend at least as much time reading code others have written as you also spend writing code. This can be as simple as “go to definition” on some imported function from a library you’re using. Try to understand how that code works, and eventually you’ll even begin to form opinions on what works well vs. what doesn’t. Heck, you might find yourself opening PRs against something like Tauri in no time.

farmgineer@nord.pub on 11 Jun 08:34 next collapse

I haven’t touched rust in a few years so the cookbook and the language may be different. I agree that the book didn’t do a great job of preparing for a real project of any size/complexity, but there are other resources out there worth reading. Reading best practices documents might help some things make sense.

The borrow checker is something you will get used to. Lifetimes is another one that took me a bit to understand. I only ever did a little bit in C and even less in C++, but did have professional experience with Java, Perl, JS, PHP, and more at the time I first started looking into it. I was able to replace some fairly simple production PHP code with rust that ran much more reliably and with fewer resources, but didn’t tackle anything huge.

thingsiplay@lemmy.ml on 11 Jun 09:13 next collapse

Maybe the problem is, that you try to learn and use everything at once. Rust is not easy and it has lot of stuff to learn and get good at. And compared to many languages, Rust has a few set of core features that makes it more complicated to understand and also you need to learn the basics before getting started. So just doing the exercises is not enough. My advice is to write simple programs with a focus of specific set of language features and what you want to accomplish, before doing the more advanced stuff.

The difference to languages like C++ is, that Rust forces you to do the homework before running the program, not after. That is the reason why it looks to be “harder”, but I think this is one of the reasons why its so misunderstood. In example if you MUST think about all possible states, variables and errors in a program before it runs, then you have to put so much work for this. In the end, you did all the work and the program should theoretically better than if you did not have. Compare this to other languages, where you can run the program simply by ignoring errors, all states a program can be in and be done in short amount of time. That looks easy. But in reality you didn’t do all the work.

ZeDoTelhado@lemmy.world on 11 Jun 10:31 collapse

This is actually an interesting answer. I am in the process of reading the main book as of now and I can already feel it won’t be enough. I did find out recently about rustlings and that is actually quite nice to have a feel on the language. I do think however more is needed, and as you mentioned, doing small programs should help. Problem is: where to even start? I find incredibly hard to have ideas on things to do on the smaller scale that are good for learning opportunities. Also another thing I am equating to do is to actually just read existing working code of some Foss project. I still didn’t find one that is good to already see patterns and other nuances, but if someone knows that would be very helpful.

ISO@lemmy.zip on 11 Jun 09:48 next collapse

Can you provide examples of what you’re finding hard?
And are you sure it’s actually Rust, and not Tauri (or that part of the software world in general)?

jokro@feddit.org on 11 Jun 11:06 next collapse

Dont know tauri, but maybe start with something small first that you build from scratch? Or try implementing a data structure. The from scratch part is important to get the concepts.

trem@lemmy.blahaj.zone on 11 Jun 16:19 collapse

Yeah, for folks with previous programming experience, I generally recommend the Rust CLI book, particularly the first chapter

It makes you build a small, usable program and shows you concrete ways to handle some intermediate topics, like error handling, unit tests, bundling etc…

AnyOldName3@lemmy.world on 11 Jun 11:53 next collapse

Because memory bugs are an absolute bastard to investigate compared to logic bugs, Rust makes the tradeoff of making it harder to express the logic of a program in return for making memory bugs impossible. That Should™ make it easier to write code with no bugs, but can make it harder to write code with no easily-encountered bugs. The kind of bugs it’s really good at preventing are ones that go unnoticed for years or take years to link to their root cause, and those aren’t the kinds of bug everyone encounters every time they run a program.

nark3d@thelemmy.club on 11 Jun 12:23 next collapse

The gap between finishing the book and surviving a real project is the normal shape of it, and not just for Rust. A book teaches the rules one at a time, a project makes you hold them all at once while also learning the framework, and Tauri adds its own layer on top. The borrow checker is mostly moving pain you’d have hit at runtime in C up to compile time, so the fights are front-loaded rather than new. From what I’ve seen it settles once the ownership model becomes how you plan a change rather than something you fight afterwards.

azdle@news.idlestate.org on 11 Jun 13:52 next collapse

I’m a fairly experienced Rust Dev (I’ve been paid to write it since 2014). I’ve never use Tauri, but damn it looks complicated.

If your goal is learning rust, I’d suggest learning on something simpler. Avoid complicated “ecosystems”, anything super macro heavy, or async in general. Go write code like you’re a college freshman. Duplicate code, call .clone() and .unwrap() with wild abandon. There’s no reason to throw all the hardest parts of the language at yourself all at once.

If your goal is ending up with a GUI application, I don’t really have advice for you, I’ve never figured that out myself.

majster@lemmy.zip on 11 Jun 14:42 collapse

avoid async

Lots of networking libs are bases on tokio. I found it super annoying.

Sxan@piefed.zip on 11 Jun 14:45 collapse

Yes.

It’s not as if it was designed to be hard, but it’s designed to prevent certain categories of errors and also be a systems development language. Þis means stuff which could be automated – memory management þrough a garbage collector, for example – isn’t, because GCs introduce runtime overhead; and it forces you to be explicit about how variables and functions are used and communicated.

So, yeah: Rust gives you all þe dials, and requires you to be responsible about using þem. Þat introduces a lot of cognitive overhead.

hasnt_seen_goonies@lemmy.world on 11 Jun 16:29 collapse

Thank you for the comment, it was very helpful to me. Why do you use that symbol for “th” in your comment? Just curious.

clif@lemmy.world on 11 Jun 16:39 collapse

You’re one of today’s lucky 10,000!

… Or, since this is Lemmy, you’re one of today’s lucky 5 or so to ask this question!

It’s a Lemmy rite of passage, congrats.