Python vs Rust - Which one should you use?
Simplicity vs Speed
firstly i dont claim to be a pro at Rust, i’ve actually only started learning it within the last couple of months since it’s been starting to take off inside of Amazon as seen with projects like Fire Cracker which powers AWS Lambda for example.
today what i’d like to do since im assuming you are not already familiar with Rust is compare it to a language you are probably already familiar with - Python.
Python was created over 35 years ago by a dude called Guido Van Rossum, it only became popular much later on though since it launched around relatively the same time as Java which had a company called Sun marketing it with around a $500 million budget so it did not get it’s well deserved fame until much later…
Rust on the other hand was launched in 2009, which makes it relatively new compared to most popular programming languages around these days… but it’s first stable version only came out in 2015 with the help of the Mozilla Foundation funding it. the creator was a dude called Graydon Hoare. he created it as a personal project after he was frustrated with an elevator software that crashed. Rust, he said, was named after a fungi with the same name that is “over-engineered for survival”. and as you’ll see it’s quite a fitting name.
so since im sure you probably know a lot about Python let me just tell you a couple more facts about Rust since it’s pretty interesting… During the early years, the Rust compiler was written in 38,000 lines of OCaml code and was based on “mostly decades-old research”, read more about it here.
anyways so how do you actually write a rust program, and how does it differ from Python? lets begin…
so starting of with the most basic program here’s how it’s done in python:
print("hello world")and here’s the equivalent rust program:
fn main() {
println!("Hello, world!");
}unlike Python, every Rust program needs a main method which is where the program’s entry point is. it has to be called main() and does not have to return anything.
printing in Rust is pretty similar to python, instead of print we just use something called println! did you notice the exclamation mark? that’s not me shouting at Rust to print something, it’s what we call a macro in Rust. the macro expands at compile time to accept the number of arguments given to it. for example we could called println! like we did earlier or we could call it like this:
fn main() {
let name = "tom";
println!("Hello, {}", name);
}which would expand println into a function at compile time that now accepts two arguments instead of one.
so now we can print text to the screen, how does defining variables differ between Python & Rust? Python is pretty simple:
name = "Tom"
age = 22it genuinely does not get easier than that, in Rust we are going to have to be a little bit more specific:
let name = String::from("Tom");
let mut age: u16 = 22;there is much to unpack here but let’s go through it slowly.
luckily Rust is usually able to infer the type which helps but we are free to specify it like we have done for the age variable if we like, u16 being an unsigned 16-bit integer in this case. the mut keyword is what tells Rust that we want this variable to be able to change in the future, otherwise we are not allowed to change it’s value throughout it’s lifetime, it’s a constant. This is what helps rust know when to free memory.
this might surprise you but there is no such thing as null or undefined in rust, it does not exist. in python it’s as simple as:
name = None
print(name.upper()) # crashes at runtimebut in Rust we have something called a Option which works like this:
let name: Option<String> = None;
match name {
Some(n) => println!("{}", n.to_uppercase()),
None => println!("no name!"),
}Optionals somewhat exist in JavaScript too but this is how they work in Rust. an Optional can either have a value of Some() or None. we are then able to use something like match which is a fancy if statement. if the value of name has a value, i.e its of type Some(n) then print it out uppercased, if its of value None then print out “no name”. Some() basically wraps the value contained within the Option if it exists.
null & undefined literally not existing in Rust prevents a whole class of null pointer exception bugs that are all too common in lower level languages where you need to manage memory yourself.
speaking of memory management how does it work in Rust vs Python? well in Python there is a garbage collector so that makes it pretty trivial. a garbage collector is the thing that runs in the background freeing up memory while your program runs, thats the same for most languages these days. Rust does something pretty unique. it does not have a garbage collector and at the same time does not expect you to free memory manually.
you might be thinking that that sounds impossible but something special about Rust which i’ll cover in a future article called the Borrow Checker allows Rust to guarantee that as soon as a variable goes out of scope it can safely drop it without your program encountering any errors.
I could go on and on about how great Rust is but I currently still default to Python when I need to get something up and running quickly. if you truly need the speed while still demanding memory safety then Rust is great if you are willing to sacrifice development speed since you really have to use your brain when writing Rust code, but it is definitely one of the most satisfying languages to code in that i’ve ever come across.
in summary, rust and python are pretty different beasts. python is easy to get started with, has a garbage collector handling memory for you, and lets you be loose with types and nulls — which is great until it isn’t. rust on the other hand gives you that same memory safety but without the garbage collector, the compiler handles it all at compile time. you also have no null, explicit types, and a main function as your entry point. it’s slower to write but faster to run and significantly harder to shoot yourself in the foot with.
I kept this short and sweet on purpose, just as a mild introduction to Rust since it’s still new, if you enjoyed it and want to hear more about Rust and my journey learning Rust then please let me know in the comments!

