2022-02-05 06:03:01

The compilers course I'm planning on taking will use Rust for programming projects. I'm getting a head start by reading the [
official Rust book
. I know there are lot of Rust opinions that have floated around here that I couldn't comprehend because I wasn't familiar with the language in the slightest. What do I need to be aware of while learning Rust? I'm certain I won't be using anything particularly complicated in this course that necessitates even a fairly proficient-level understanding of Rust practices, but I also don't want to bootstrap myself by adhering to bad conventions while learning the language.

What game will hadi.gsf want to play next?

2022-02-05 06:25:22

Honestly the rest comes from experience.  That said if this is a compilers course you'll probably end up learning most of it, if only because any halfway good compilers course is going to have you write a compiler and that'll need data structures and things.

The reason people get into trouble--and this includes me as well as everyone else, if you didn't you're lying or not there yet--is that the single mutable borrow and lifetimes make thinking about ownership super super important.  Not only that but you have to think about it in what I can only call a Rust way, and I honestly cannot articulate what that means and just have to advise you to think through what the lifetimes/borrows are going to have to be in your interfaces, 1000 lines down the road.  Potentially hard to learn, easy to do once you have. (also, note that interior mutability is a thing, so you do have an escape hatch. But save for concurrency and I/O it's kind of bad practice to use it when unnecessary).

My Blog
Twitter: @ajhicks1992

2022-02-06 22:11:33

Camlorn is correct. The rust way is basically "do it our way or get the fuck out". Which takes a long time to get used to and there's certainly a learning curve but once you get it, I imagine it becomes second nature, just like any other task.

2022-02-06 23:42:31

I mean, not really?  I wouldn't put it that way.  You do have a lot of expressive freedom. You can do something like OOP if that's what you really need right now for instance.  It's not built in but it's not hard to design with macros and Synthizer spun up a whole Synthizer-specific class system with downcasting and all that in like 100 lines of macros and an hour of work.  But it's a language for the C/C++ application space and a lot of what it does is something along the lines of "we don't let you do this thing you could do in C, but if you do the thing in C you'll probably have bugs anyway".  But where it really shines is things like threading, where you just kind of get to go have at it without issue save perhaps for deadlocks, and fast networking, because they really, really got their async networking story right.  But the borrow checker is a thing, and so is the fact that it's more along the lines of the functional languages and in fact the earliest versions of the compiler were written in Ocaml, I believe.

there's lots of things I can express in Rust with zero performance overhead and in a quite readable fashion that Python can't do for example.  It's not "our way or get the fuck out" that's unique to Rust, it's that the Python way is so natural to most people due to being the dominant paradigm that even though Python also has a "get the fuck out", you're so used to it that you don't notice that it does.  Python likes classes.  Rust likes monadic composition and readable iterator chains.  You can't really do either in the other language well (it can be done, just not without significant cognitive overhead).

You can also drop into unsafe Rust at any point and unsafe Rust is C plus some goodies pretty much and can generally do anything C can exactly how C would do the thing.

My Blog
Twitter: @ajhicks1992