this post was submitted on 05 Apr 2024
43 points (95.7% liked)

Learning Rust and Lemmy

231 readers
1 users here now

Welcome

A collaborative space for people to work together on learning Rust, learning about the Lemmy code base, discussing whatever confusions or difficulties we're having in these endeavours, and solving problems, including, hopefully, some contributions back to the Lemmy code base.

Rules TL;DR: Be nice, constructive, and focus on learning and working together on understanding Rust and Lemmy.


Running Projects


Policies and Purposes

  1. This is a place to learn and work together.
  2. Questions and curiosity is welcome and encouraged.
  3. This isn't a technical support community. Those with technical knowledge and experienced aren't obliged to help, though such is very welcome. This is closer to a library of study groups than stackoverflow. Though, forming a repository of useful information would be a good side effect.
  4. This isn't an issue tracker for Lemmy (or Rust) or a place for suggestions. Instead, it's where the nature of an issue, what possible solutions might exist and how they could be or were implemented can be discussed, or, where the means by which a particular suggestion could be implemented is discussed.

See also:

Rules

  1. Lemmy.ml rule 2 applies strongly: "Be respectful, even when disagreeing. Everyone should feel welcome" (see Dessalines's post). This is a constructive space.
  2. Don't demean, intimidate or do anything that isn't constructive and encouraging to anyone trying to learn or understand. People should feel free to ask questions, be curious, and fill their gaps knowledge and understanding.
  3. Posts and comments should be (more or less) within scope (on which see Policies and Purposes above).
  4. See the Lemmy Code of Conduct
  5. Where applicable, rules should be interpreted in light of the Policies and Purposes.

Relevant links and Related Communities


Thumbnail and banner generated by ChatGPT.

founded 7 months ago
MODERATORS
 

Hey!

I'm a professional software engineer with several years of experience using Rust. Unfortunately I don't really have the time to contribute to Lemmy directly myself, but I love teaching other people Rust so if:

  • You are curious about Rust and why you should even learn it
  • You are trying to learn Rust but maybe having a hard time
  • You are wondering where to start
  • You ran into some specific issue

... or anything to do with Rust really, then feel free to ask in the comments or shoot me a PM 🙂

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 6 points 5 months ago* (last edited 5 months ago) (1 children)

Is it possible to become as productive with rust as one is with higher level languages?

Definitely. Rust can be very high level, just as or at least very close to the level of languages like Python. Just as high level as Go or Java I would say.

Rust is quite a wide language, in the sense that it is able to do both low-level and high-level programming. More generally, I'd say the distinction between "high level" and "low level" is just a matter of how much abstraction you use and you can build very powerful abstractions in Rust, and people have done this in many crates.

For instance while using the axum web server framework, you might see code similar to this:

async fn new(db: State<PgPool>, value: Json<MyType>) -> StatusCode {
    ...
}

This function defines a HTTP endpoint that takes a JSON body that deserializes to MyType and internally accesses a PostgreSQL database (for example to save the value in the database) and returns an empty body with a given status code. This is a very "dense" definition in the sense that a lot of semantics is pressed into so few lines of code. This is all possible due to the abstractions built by the axum web framework, and you see similar things in many other crates.

I was kinda hoping to make rust be my go to language for general purpose stuff.

It is a great language for all kinds of stuff - I use it as my general purpose language all the time :)

I would encourage you to stick to it and see how the language feels once you try it a bit more in a real project, and not just in exercises. The book can be a little dry because it has to explain both the high level and low level features of Rust.

If you have more specific questions about what exactly makes it awkward, maybe I can provide more guidance.

[–] [email protected] 2 points 5 months ago (1 children)

General Purpose Language ...

Interesting @[email protected] , I don't think I'd personally every thought of having rust as a go-to general purpose language instead of something higher level like python etc. I'd always presumed it'd be the tool for higher performance tasks/libraries when desired (where the enforcement of safety and the "work" necessary to "pass the compiler" and the type system are desirable features are, to me, desirable features).

But so far, I've seen enough to appreciate how rust, once you know it and the packages/crates and std library, can feel kinda high level ... so an interesting prospect to watch out for.

Exercises/Challenges to supplement "The Book" ...

My personal experience with the book hasn't been fantastic. It's too dry and "academic" IMO and could really do with a more hands on hacking stuff together complementary book. Which is odd because it opens with a good example of just diving in and writing a small program.

So yea, I'd echo SorteKanin's suggestion of writing programs as an exercise. I've tried to promote the idea in this community by posing challenges. I've got a "portal post" for them (only 1 so far) here: https://lemmy.ml/post/13418981. The first was writing a "file differ". I did it (you'll see the solution linked in the portal) ... it took longer than I'd preferred but definitely made me feel like I was learning way more than with the book.

I have a strong feeling that doing more of that sort of thing, including Advent of Code, maybe euler problems (are they still cool?) or whatever else people are interested in (I'd like to have a shot at hacking together a basic web app) ... would be really helpful here (thoughts?).


Tips on Rust as a General Purpose Langauge?

@[email protected] ... do you have any thoughts to share on how Rust is best approached as a higher level / general purpose language? We discussed elsewhere in this comments section the idea that "over-optimising" memory management is unnecessary ... that outside of hot loops you should just happily clone variables to make the borrow checker happy. Any other tricks or approaches you can think of?

Does unsafe mode become a tool worth using, or is it not worth the hassle? What about the Reference Counted pointer (smart pointer?) or Arc<Mutex>s (which I haven't come to learn/understand) for handling memory management?

Are there good and nice crates or standard library tools that aren't the most efficient or idiomatic but good for just getting stuff done?

Also, thanks for answering all of these questions!

[–] [email protected] 3 points 5 months ago (1 children)

do you have any thoughts to share on how Rust is best approached as a higher level / general purpose language? We discussed elsewhere in this comments section the idea that “over-optimising” memory management is unnecessary … that outside of hot loops you should just happily clone variables to make the borrow checker happy. Any other tricks or approaches you can think of?

I would say make good use of the crates available. As said above, Rust as a language allows for very powerful abstractions. The language itself is quite low level, if you didn't have any other code to use. But the standard library alone gives you a lot of tools. Add to that a host of practical crates that raise the abstraction level and you've got a very high-level experience.

I can't not share this meme btw. It's obviously a joke but every joke has a bit of truth:

For instance, you've got anyhow that allows for a bit of "quick and dirty" error handling via the ? operator. It's useful for cases where you don't care too much about what error happens, just that it happens. And when you get an error, you get a nice explanation of what happened (no 100 line exception stack trace with 1 needle in the haystack that maybe tells you what went wrong).

There's also serde (by the same author even) that allows for very easy serialization and deserialization, for instance into JSON or other formats.

You could take a look at lib.rs to find more crates within a lot of different areas. It's a bit more categorized and better sorted than crates.io. Learning the ecosystem of crates and what's good for what is kind of a secondary thing to learn for Rust (or any language with a package ecosystem really) and it will take some trial and error probably.

Does unsafe mode become a tool worth using, or is it not worth the hassle?

No, unless you seriously need it and you definitely know what you're doing, you don't need it and you shouldn't need it. If you're reading stuff on this community, you don't need unsafe. I've worked with Rust for many years and I've still only used unsafe in the areas where it's really needed, like FFI (calling into C or C++ code). Perhaps with embedded programming you need it more, but most people don't do much embedded.

What about the Reference Counted pointer (smart pointer?) or Arcs (which I haven’t come to learn/understand) for handling memory management?

You should definitely learn about Arc (Atomic Reference Counted pointer) and Mutex (mutual exclusion lock) - I believe the book has a chapter or something about them. They provide one way to achieve "shared" ownership across different threads and you'll probably have lots of headaches if you don't know about them.

Are there good and nice crates or standard library tools that aren’t the most efficient or idiomatic but good for just getting stuff done?

There's some I mentioned above and for most major use cases, there are crates. Like axum for web servers as I mentioned above. Look at lib.rs I would say. It really depends on what specifically you want to do though.

[–] [email protected] 2 points 5 months ago

Thanks!

And great little meme there!