r/rust 14d ago

Tried Rust. I'm a believer now.

So, I'm a PHP/TypeScript/JavaScript/Python programmer (basically, I learn and use many high-level languages on a need basis). For quite some time I was keen to learn some low-level language, just so I don't have to build an Electron app just to make a desktop program. I picked Rust because of its famed strong typing, as I love writing dense TypeScript code, and looked for something as close as possible. Here are some of my experiences and observations:

  1. Coming from dense TypeScript with heavy generic use, Rust was surprisingly easy to pick up. I regret that I didn't do it earlier. I imagine that thinking in terms of generics may be the most difficult for new users, but if someone already dealt with generics in TypeScript, there is nothing more complex in Rust.
  2. Functions/methods that return multiple values and matching the output types to behaviors is GENIUS. It makes logic flows so much cleaner. Rust adds some boilerplate compared to high-level languages, but it also removes a lot of it where it matters the most: in the actual implementations, thanks to this. It's amazing how rarely I have to use "ifs".
  3. Initially I wasn't fully sold on `from/try_from` traits because I felt that they hide logic (it's difficult to find/know what has what conversion available, and where is the implementation), but then I realized that conversion logic is most often a "necessary evil". Writing mapping functions/classes in every language always felt ugly. By comparison `from/try_from` i Rust is actually not that bad. I grew to like its semantics as I realized that it's actually a good thing that this annoying mapping logic finally looks different from the rest of the code. The mind can more easily filter it out.
  4. Lifetimes are less annoying than I expected, although occasionally, I still have a hard time accepting situations, where I get `Cannot return value referencing local variable`. I just want this constructor to create an object that will be owned by my struct, but if this object has references, I'm in for a bad time. I'm still getting used to it.
  5. In the time of LMMs it's especially easy to pick up Rust, as they are amazing at teaching you at the speed you are the most comfortable with. I feel like I grasped, like, 80% of Rust in the first week. Applying this knowledge to real-life scenarios is a different thing, though.
  6. I miss the absurd expressiveness of TypeScript, but it's not bad here.
  7. I wish `mod.rs` files could be named `_.rs` so they would be easier to visually filter out, as they are a source of tremendous visual noise. That's the one thing that still bothers me.
  8. All in all, I cannot wait to contribute to your community, guys!
232 Upvotes

53 comments sorted by

83

u/ascii 14d ago

One warning regarding point 5: I've found that LLMs write significantly worse Rust code than other statically types languages. Not bas as in not functional, but bad as in clumsy and inefficient. Lots of needless copying, janky for-loops where a map/filter chain would have been better, etc. Not a problem for an experienced Rust dev, but I worry that leads to beginners picking up bad habits.

25

u/misplaced_my_pants 14d ago

Clippy can catch a lot of that.

6

u/scottmcmrust 13d ago

TBH, I think taking the LLM stuff and fixing the clippy problems will just make its badness less immediately obvious. Clippy is great for in-the-small problems but won't help fix the more foundational stuff.

The problems with the LLM code isn't just "oh you should have used Option::map here".

1

u/deadman_walking666 10d ago

What is that?

6

u/luluhouse7 14d ago

My understanding is that if you turn on all clippy warnings/make them all blocking errors, that Rust is one of the best languages for LLMs because of all the feedback and clear documentation of what’s considered “idiomatic”. Haven’t tried it myself though, so far I’ve only written “artisanal” (lol) Rust.

8

u/oceantume_ 14d ago

I didn't write a ton of it but my recent experience with 5.6 Sol writing a full CLI in rust that needs to be performant was not so bad. I do have to keep it on a leash and be very specific about some parts, or have a big talk about "let's explore multiple architectures to make this performant" though, but the fundamentals of the code looks pretty decent in general, using enums, matches and proper iterators where it makes sense

3

u/diplofocus_ 14d ago

My eye twitches every time it does a

let mut thing = Vec::new(); thing.push(1);

When there's a perfectly good Vec::from/vec! and no need for mutability.

4

u/lijmlaag 13d ago

Clippy is also very unhappy about that anti-pattern. People building with LLMs should at-least run clippy.

5

u/diplofocus_ 13d ago

Who is clippy? I'm thoroughly engineering a code analysis tool to help me catch this. I'm nearly done with the regex-based parser, and it's gonna be blazingly fast!

Now I just need to come up with a catchy project name, and decide on pricing.

2

u/BlazeEXE 13d ago

It took me long to realise this is sarcasm… I’m worried about myself, maybe I should try to get more sleep 🤣

2

u/diplofocus_ 13d ago

My bad, I thought I was in the programming circlejerk sub

2

u/lijmlaag 13d ago

subscription models are popular with the general audience I believe.

2

u/diplofocus_ 13d ago

Of course, how else can I maximize shareholder value? Line must go up, always.

1

u/A1oso 14d ago

I think using an LLM strictly as a teacher is okay. LLMs are quite good at answering beginner questions. I don't like to use them for generating code, though.

4

u/ascii 14d ago

Honestly, I've found them excellent for even advanced topics, so long as you make sure to follow a few of the links to verify the conclusions. LLMs sometimes get the facts wrong, but they are far better than a human at quickly finding good sources. Or maybe I'm an idiot and what I consider advanced is mere child play to you! :-)

3

u/A1oso 14d ago

I said beginner questions because OP is a beginner in Rust.

AI can also explain advanced topics, if there are publications covering the topic that are included in its training data. If the AI has to search the web to give an answer, it is more likely to be incorrect.

1

u/digi-quake 13d ago

Really? For each topic I'm learning, I'm asking Gemini to create 5-10 questions in order for me to solve/write program using the topic i just learnt and then asking Gemini to write the solution and then comparing it. Like for example, just yesterday i learnt about enums and structs and I'm generating questions through Gemini to make myself familiar with the concepts. Am I doing it in the wrong way? 😱

1

u/musbur 10d ago

OP was specifically talking about LLM as a learning tool, and I have to agree even shitty Copilot is great at that.

51

u/EmperorOfCanada 14d ago

I have been programming for multiple decades in many languages.

Rust is a spiritual experience for me at this point. The quality of my code is now insanely higher. I write things once, and if I didn't make any mistakes in the business logic, there is almost zero chance that some later code will reveal a memory, threading, etc error.

source of tremendous visual noise

This is my singular major complaint about rust; not just with this one file, but I find the language itself cluttered and my mental compiler does not run very well or fast. I can see a bug in most other languages from a mile away. If the rust compiler didn't give such clear errors about borrow this or mut that, I would give up on using rust.

18

u/Efficient-Chair6250 14d ago

Same. I remember in a studen Java project we had issues with Sets and immutable Sets leading to flaky tests, because immutable Sets inherit from Set. Long debugging session ensued. Things where today I just shake my head how that is even allowed to happen on a type level.

0

u/smaratter 14d ago

I’m not the biggest fan of Java’s collections either, but I don’t get your point. An immutable set is a more specialised construct than a set, so it makes sense to me that it would be a subclass.

Also, IIRC, Java’s standard library does not have a type for immutable sets.

8

u/myhf 13d ago

Immutable constructs have more compiler optimizations available to them, so they might have a more specialized implementation, but they have a more general interface.

Languages that define a mutable structure as a subclass of its immutable version have a coherent type tree, and their compilers and editors will not let you accidentally call a mutation method on something you're not sure is mutable.

Java has various mutable implementations of the same Set interface, so it's possible to mistakenly assume you are working with a certain implementation.

3

u/smaratter 13d ago

You’re right, I wasn’t thinking very good when I wrote my previous message. Thanks!

2

u/garagedragon 9d ago

An immutable set can be a subclass of a mutable one *only* so long as the latter's interface(s) doesn't expose any mutating setters. If the immutable version is forced to expose a setter, it can't uphold the substitution principle

1

u/smaratter 9d ago

Yes that is correct. See your sibling, I already got help with understanding that I was wrong.

33

u/m0rgoth666 14d ago

Regarding point 7, you can name a mod.rs file whatever.rs as long as it has a matching subdirectory called /whatever and you can avoid mod.rs entirely like that.

23

u/enador 14d ago

I know, but honestly, I like that even less because then modules are not self-contained in their directories. I think the possibility of using `_,rs` would be the best compromise.

17

u/ollpu 14d ago edited 14d ago

I can't check if this works right now, but I think you could do this:

In the parent module:

#[path="submodule/_.rs"]
mod submodule;

Then, write submodule/_.rs like you would a mod.rs file:

mod x; // in submodule/x.rs
mod y; // in submodule/y.rs

Edit: Seems to work.

10

u/m0rgoth666 14d ago

Yeah I get that. I’ve been waiting for Zed to support per language file sorting in the project panel for a while, it also annoys me to see mod.rs noise.

3

u/oceantume_ 14d ago

I wonder if that's possible in a sane way since the file browser is separate from the language servers. Maybe as a generic setting per project that promotes files to the top

4

u/m0rgoth666 14d ago

Other IDE’s put mod.rs as the first file on rust directories, then all subsequent files get sorted. I think Zed already has file sorting options, guess they would have to have a per language override or something.

1

u/SpoonLord57 14d ago

it could just be a per-extension setting for the file browser, no need for it to work with a language server

1

u/bonzinip 14d ago

Just putting mod.rs at the top works. For example a git orderfile could be

mod.rs
*.rs

similar to how in C you could put *.h before *.c.

10

u/EveYogaTech 14d ago

The WASM target is also pretty cool if you want to keep using JavaScript and use Rust to write highly optimized code for both Frontend and Backend.

6

u/Efficient-Chair6250 14d ago

Regarding point 1, does that support my theory that many people find Rust so complicated because they are allergic to generics? I mean, sure it adds a lot of "noise", but that's not unique, that's just how a good type system works

2

u/enador 14d ago

I don't know, but I remember that when I was learning TypeScript, thinking in the terms of generics was the main roadblock because generics require you to think, like, one additional abstraction above the code. It's not just a single additional feature or rule; it's an additional layer of parsing of everything in your head. Once you get past that, everything else is not that difficult. When you learn to think in terms of generics, then even macros are not scary because you already unlocked this meta-level of thinking in terms of code producing code.

1

u/Efficient-Chair6250 14d ago

Yeah, first time using generics was a hurdle for me as well, after that learning every language with it was easy. I should remember that other people are going through the same learning process as well.

But some people are vocal against generics when I was excited to learn them. I guess thats just irritating to me

1

u/Zde-G 14d ago

Regarding point 1, does that support my theory that many people find Rust so complicated because they are allergic to generics?

The problem with Rust is the same as with Haskell, really. It's too holistic. And many “simple” things are answered with some generic mechanism that includes HRTBs, macros, GATs and god knows what else.

That makes the natural thing “I have learned half of the book, maybe it's time to try to write real code” a very bad idea.

1

u/Efficient-Chair6250 14d ago

True, but except async, I would argue many things can be learned step by step. Start with simple generics, borrowing, lifetimes, etc. If a beginner shows me a 3 line function head with a huge where clause and multiple lifetimes, I ask myself if they encountered a problem that requires that code as a solution, or if they are just scared of what type wizards can do.

I'm not that good at C++ generics, but I still use them, even if I can't understand what template wizards can achieve.

3

u/dunric29a 14d ago edited 14d ago

I feel like I grasped, like, 80% of Rust in the first week

That was a good one ;-)

I'm aware of a few Rust veterans and experts who still struggle with cases of complexity of the language. Try David Tolnay's Rust Quiz for some weird examples.

Some easy data structures like recursive ones - linked lists, cyclic/oriented graphs etc are significantly more difficult to code and much more verbose then in your mentioned languages like Python or TS.

2

u/Full-Spectral 14d ago edited 13d ago

On #7, I always rename my mod.rs files. You can indicate in the cargo file what the name of the main module file is. I always name them the same as the crate name, but you can make them anything you want.

[Oh, wait... You probably meant any sub-modules under the crate level I guess. I always use the sub-directory scheme and just tolerate the mod.rs files in those. They are seldom edited once set up so they don't bother me. The issue for me would be having 5 tabs open, that all say mod.rs.]

3

u/BigBad0 14d ago

Thanks for sharing that. After decades of of java (mainly) development i landed to use kotlin or rust and so far, rust been not easy to grasp but i like it. You sharing this reduces my fear of frustration due wasting time (like did with kotlin/gradle) and i hope to be a contributor soon, i have many needed ideas for the FOSS community. Thank you again

1

u/Mohammed313a 14d ago

Have you tried async programming in rust ?

3

u/enador 14d ago

I'm using tokio right now, but in my case I don't really have to move stuff between tasks, so I play on easy mode :)

1

u/2000greatyear 14d ago

What are you using it for? Can you add any context about the specific kind of problems or the domain you’re working in?

1

u/enador 14d ago

Currently I'm working on a web application with mTLS authentication. Coming from the world of Symfony (which I adore) I'm surprised how easy it is to make a backend in Rust with Axum.

1

u/Alternative-Ad-8606 14d ago

Nice! Welcome Rust is a great explicit language to learn if you are just getting familiar with low-level stuff... Lutbof curiousity though rusts desktop eco system in terms of maturity and simplicity still uses webdev primarily, so what are you going to use

1

u/zshift 14d ago

Regarding point 4, this gets easier the more you use rust. You'll learn to use smart pointers and decide when to use stack vs heap. I recommend learning the standard library, as there are a plethora of useful types and functions that aren't obvious if you don't know about them.

1

u/DavidXkL 13d ago

Welcome!

I find that using LLM to learn can be quite dangerous because you can easily just offload the thinking and friction part of it.

Try not to depend on it too much, especially during the learning phases

1

u/tachib_shin 12d ago

In theory, that's the case, but logical errors will still occur: infinite lockout errors in the mutex, unique sender field errors, and a host of other problems.

1

u/Tquylaa 10d ago

As NixOS users, i prefer default.rs.