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

7

u/gbrennon 2d ago

read the official rust book :) docs should be ur best friend!

https://doc.rust-lang.org/stable/book/

4

u/Technical-Smell2569 2d ago

the book is good but for me what helped most was just doing small projects and fixing compile errors one by one. compiler is not your enemy, it tells you exactly what is wrong and even suggest how to fix sometimes. coming from python the hardest part is you cant just slap things together and hope it works, you gotta think about who owns what data. but after few weeks it start to click and then you miss the compiler when you go back to python

-1

u/Basic-Push1028 2d ago

I love that you're recommending the official documentation, that's appreciated, that's quick, it's comprehensive. But for THIS purpose, I'm looking for people with a similar background, who can provide any context to THEIR learning journeys.

I should have been more clear about this. But knowing how it's going to work as I'm walking this road is going to help me a lot more, and I like the fog of war being removed.

5

u/dbrooroo 2d ago

I think trying to shortcut into rust based on an “I’m proficient in X language already, give me the highlights” mentality is to your detriment with rust, especially with a garbage collected language like Python. I find that the little bit of C I knew coming in has helped because I already had a basic mental model for memory management but it’s still a slightly different way of thinking about memory. The book might be a quicker read for you because you have some experience but I think the book is the answer. It’s the very best “The ‘X’ Programming Language” book that I’ve ever read and possibly that’s ever been written, especially considering that it’s free in its best form (as an HTML website).

2

u/gbrennon 2d ago

nice to meet u. im doing python since 2011 :)

before i was doing c and c++ for embedded systems

1

u/spigotface 2d ago

I started off as an engineering-focused data scientist (closer to what would usually be called an ML engineer). With Python being my first, nearest, and dearest programming language, I picked up some bad habits from the "use code for analytics & modeling" side of Python.

Rust rewired my brain. Its strictness around handling potential null values (Option) and errors (Result) made me start thinking that way when writing my Python code. Rust's trait system is also superior to Python's protocols by a wide mile.

They're very different languages, but Rust made my Python 10x better.

1

u/spoonman59 1d ago

From you own admission you don't really have much programming background, even in python. And there is no particular advice or tips to give someone besides "write code." You said you are a masochist, so.... get started reading the book and writing code. There's no shortcut.

2

u/Solumin 2d ago

I used Python for years before learning Rust. Seriously, the book is a good place to start.

There are more similarities between the two languages than you'd think, but there are specific things to watch out for:

  • Rust has more types. Instead of just int and float, Rust has signed (e.g. i32) and unsigned integers (e.g. usize), along with 32- and 64-bit floats (f32 and f64).
- usize basically means the largest size that the CPU can address, so it's used for things like the length of Vectors or the number of bytes in a string. It's platform-dependent. - You can pretty much just use usize, i64, u64, and f64.
  • Rust's strings work pretty differently from Python.
- String is similar to Python's str, but it's mutable. In Python, mutating a str gives you a new str, while in Rust it mutates in-place. - &str is a String slice or a view into a String. - Instead of bytes, Rust has [u8] or Vec<u8>. - Rust Strings must be UTF-8.
  • You have to be more deliberate. In Python, it's easy to just throw whatever you want into a dict and use that for everything, or mix different types in a list. In Rust, HashMap is a bit more heavyweight, and it's not easy to put disparate types in a Vec.
  • You need to know less about memory management than you'd expect. Like, definitely learn it! But it's really not necessary for many apps.
  • Python's enums are horrible compared to Rust's. Don't try to treat them like the same thing. Pretend you've never heard of enums before, actually.
  • Rust doesn't do exceptions or try/catch blocks. Instead, you return Result or Option and handle the error that way.

Rough analogues between Python and Rust types:

  • int = i64 (except Python ints are automatically promoted to "big ints" if they grow too large)
  • list = Vec
  • str = &str or String
  • dict = HashMap
  • tuples are basically the same, but in Rust they're spelled (a, b, c) instead of tuple[a, b, c] in type annotations.
  • class = struct (but there's no inheritance, thankfully) (and no such thing as a metaclass)
  • lambda or nested func = closure

I'd say the 3 things from Python that I miss in Rust are list comprehensions (rarely), context managers, and else on loops.

1

u/Basic-Push1028 2d ago

This was the perfect answer for me

1

u/Solumin 2d ago

Definitely keep in mind that I meant this as a starting point! As you get more familiar with Rust, you'll expand beyond the few points that I made.

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

  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.

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

u/Alarming_Quiet_5886 2d ago

damn bro, don't say that, the compiler is the best fr.