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 🙂
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.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.
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!
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.
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.
You should definitely learn about
Arc
(Atomic Reference Counted pointer) andMutex
(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.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.
Thanks!
And great little meme there!