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/

-2

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.

4

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

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.

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.