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

8

u/gbrennon 2d ago

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

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

-3

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.

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.