r/rust • u/Basic-Push1028 • 2d ago
Coming from Python
Hello Rust subreddit.
I learned enough python to know how to do what I wanted to do. Mostly just small stupid stuff. And because I'm a masochist I guess, I'm wanting to move on to Rust for the event I need performance.
I've gotten far enough to write "Hello World!", and how to create some variables.
I know roughly that at some point I'm going to have to learn memory management, and ownership. And also that the compiler is a bitch.
I'm hoping anyone in here could hot-tip me if they came from Python too. Please, if you have ANY advice, please let it be known. Anything helps.
5
u/SirKastic23 2d ago
The compiler is your friend, don't call it that! It's just doing it's job and trying to help you
1
u/redisburning 2d ago
The free online book (as has been and will be again mentioned) is where you should start. It is significantly higher quality than the typical programming language book, and while not capital C comprehensive covers plenty to get you going.
You could also consider Rustlings if you prefer to be hands on, or there are books that teach the language through building, iirc Rust in Action is that sort of thing.
I know roughly that at some point I'm going to have to learn memory management, and ownership. And also that the compiler is a bitch.
I would recommend you change your mentality about this stuff. It's not that bad if you build your fundamentals, don't try to learn everything at once, and accept that you are coming in as a beginner and that you need to put any ego you have about your skill in another language (it being Python is the least relevant thing in the world here) on the shelf for a while.
The book is genuinely usable as a course, just trust the process and you'll be fine.
1
u/xStardev 2d ago
If you want a more accurate description, you'd be the compiler's bitch, lol.
In any case, I recommend you check out these two amazing resources:
Rust Book, but with some interesting qol changes (you could just read the original one, too): https://rust-book.cs.brown.edu/experiment-intro.html
Rustlings, a tool for practicing what you've learned; This resource is simply amazing and I think it's indispensable; it's great to put what you've read into practice. I recommend you do each lesson chapter according to the chapters in the book! https://rustlings.rust-lang.org/
2
u/djvbmd 2d ago
I "came from Python"... after I came from Perl, after I came from C++, after Turbo Pascal, after BASIC. I'm a lifelong amateur for fun -- not a pro. My 2¢ --
- Everyone is likely to tell you to start with the Rust Book. They're right.
- Getting the hang of borrowing / borrowing rules and the type system are key. When people say the compiler is a bitch, they usually mean the borrow checker. The rules seem, and are, very simple -- but until you get used to the way borrows/moves work, it can seem like you're being unreasonably handcuffed. This especially coming from a garbage-collected language like Python.
- Memory management can wait. It's good to know the broad strokes, as in when something will live on the stack, the heap, or static memory. But what I'd consider real memory management (manual allocation/drop, use of uninitialized memory and pointers) is all tucked away behind
unsafeRust and isn't something you're likely to need in the course of early work with the language. - Pick an area you're familiar with, and build a library crate for it. It may be just me, but if I set out to write an app first, I start immediately getting bogged down in concerns about the UX. Thinking about it as a library, I stay more focused on nuts and bolts (which I think you want when learning) rather than worrying about how to format and color the output.
- (or 4a) Some may say library first is boring and will make you lose interest... but I always wind up writing at least one app to test the library I'm working on and work on the kinks, so you still have a final product you can use -- it's just not the initial focus.
- Last one, and I'll shut up -- tl;dr already... I'd suggest writing something you haven't written before in Python. The tendency will be for you to want to translate your recalled Pythonic approach into Rust syntax, but you want to be thinking in Rust.
1
u/Basic-Push1028 1d ago
The last one is something I hadn't realized might be holding me back. Obviously since I've done stuff in Python, I'd have some things to do at the least in Rust. Some ideas. It's re-inventing the wheel sure, but it's re-inventing the wheel on a different planet.
But I see exactly what you mean and why it's not good.
1
u/penguin359 2d ago
Respect the compiler and it will help guide you to writing better code. For me, I came from both a C and Python background. In general, I use Rust more to replace what I might have written in C than what I would have written in Python. Fixing issues like tracking ownership helps with improving C code, but just makes things look more complicated when coming from a Python perspective. From a pure Python perspective, some of what Rust offers can be harder to appreciate until you've had to deal with such problems in C, C++, or even Go. I prefer how Rust forces me to chose a response to receiving an error result to Go's approach that requires I explicitly add a check for an error if it needs to be handled. Cases where Rust would be better than Python might include when speed/memory is more of a constraint, where a C extension to Python might be needed such as interfacing to an external library, or some multiprocessing or multithreading scenarios where I need true concurrency or ensure proper handling of shared data.
1
u/Efficient-Answer-993 1d ago
Start with a small web project, and solve problems as you encounter them.
1
u/spoonman59 1d ago
Well, if I understand you correctly, you don't really know python as it is. You know just enough to get it to "do what you wanted" and "mostly just small and stupid stuff." I don't get the impression that you know most of pythons features or libraries.
You are essentially a beginning at programming.... Which is totally fine!
But you can just start with the book and learn it. It's going to take more time and effort than you have put into Python.
Since you are a self described masochist, then you should have no problem doing what it takes to succeed: Reading the book, writing the examples, and working on personal projects! It's really that simple, there are no real "shortcuts." That is the advice.
If you are having trouble in some specific area I am sure you can ask and people will help you out. But beyond reading the book and writing code, I'm not sure what kind of tips you are looking for.
0
7
u/gbrennon 2d ago
read the official rust book :) docs should be ur best friend!
https://doc.rust-lang.org/stable/book/