Python is, according to TIOBE, the most popular language, period. People use Python for all kinds of things, from manipulating csvs to training LLMs and yet it still can be a bottleneck for building mission-critical systems. Enter Rust. Rust is getting more and more popular for jobs where you really don't want things to go wrong and performance might be a requirement.
You might think these languages aren’t related; but you’d be wrong as python regularly invigorates Rust developers:
And, by the way, it might be language the machine God is incarnated as:
Even though they're different, Python and Rust care about making things easy for the people using them. We can learn from Python's past, the good and the bad, to help Rust grow better.
Python's Popularity
Python started off as a side project by Guido van Rossum over winter 1991. People liked it because it was easy to read and helped them work faster. Python is great for newbies and experts alike.
Rust gives you memory safety, which is a big deal because it stops a bunch of common errors, but it still runs really quick.
Rust has a strong type system and checks your program so you don't have as many issues when you run it. Its community is growing with significant devotees.
Rust Learning from Python
Python has loads of tools built right in that make programming a breeze. Rust could take a page from Python's book and build up its own toolbox.
Lastly, Python shows that programs should be easy to read, which is something Rust wants as well. Keeping things straightforward helps everyone and makes programming less of a headache. Python is well-known for being quick to work with. Rust is getting there too. It's slowly adding more helpful tools, kinda like Python's tools for dealing with websites or dates and times. If Rust gets more of these, it will be easier to use right away.
Even though Rust is more complex because of what it expects you to handle, it tries to be easy to understand too. Look at this example in Rust:
```rust
fn main() {
match do_something() {
Ok(v) => println!("Success: {:?}", v),
Err(e) => println!("Error: {:?}", e),
}
}
fn do_something() -> Result<&'static str, &'static str> {
if true { // Replace this with a real check
Ok("It worked!")
} else {
Err("It failed.")
}
}
```
Rust's goal is to be user-friendly while still being fast and safe. In a way, Rust wants to be like Python: letting people get things done with less hassle.
## Python’s Problems
Python’s got a few issues though. Its Global Interpreter Lock (GIL) makes it tough to do many things at once because it only lets one thing happen at a time. This can waste time when your computer can actually do many things at once.
Here’s a Python script where, even if you try to do two things, they still wait for each other:
```python
import threading
def cpu_bound_task():
count = 0
while count < 1000000:
count += 1
thread1 = threading.Thread(target=cpu_bound_task)
thread2 = threading.Thread(target=cpu_bound_task)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
```
In Python, changing one list can accidentally change another because they're connected:
```python
original_list = [1, 2, 3]
new_list = original_list
new_list.append(4)
print(original_list) # Outputs: [1, 2, 3, 4]
```
Moving from Python 2 to Python 3 was tricky because they didn't work well together. Some people didn't want to switch, so it took ages for everyone to start using Python 3.
Rust does things a bit differently. It doesn’t have a GIL problem because it has a smart way of keeping track of who owns what in the code, making multitasking safe.
```rust
use std::thread;
fn main() {
let handle = thread::spawn(|| {
println!("Thread 1 is running");
});
println!("Main thread is running");
handle.join().unwrap();
}
```
Rust catches more mistakes before your program even runs:
```rust
let x: u32 = "I am not a number!"; // This line will not compile
```
As Rust gets bigger, the challenge will be to keep its identity and hold onto its strengths.