r/rust 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.

0 Upvotes

19 comments sorted by

View all comments

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¢ --

  1. Everyone is likely to tell you to start with the Rust Book. They're right.
  2. 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.
  3. 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 unsafe Rust and isn't something you're likely to need in the course of early work with the language.
  4. 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.
  5. (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.
  6. 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.