Rust and Python — Syntax Similarities.

Mayowa Obisesan
4 min readDec 13, 2023
Generated using Bing Image creator.

Yes, I know. Rust and Python are very different (with emphasis on very), in terms of safety, speed and usability. But in terms of syntax, both languages are quite similar.

Rust is a statically typed language, while Python is dynamically typed, although Python also supports typing. Rust is compiled, while Python is interpreted. And many more differences. Even with these differences in both languages, their syntax is very alike. Let’s look at the similarity in Syntax of Rust and Python.

STRINGS — Printing Strings

Printing strings in Rust and Python are similar, in fact, you could say very similar. Let’s look at sample code.

fn main() {
let name: &str = "Mayowa";
println!("My name is: {}", name);
}
def main():
name: str = "Mayowa"
print(f"My name is: {name}")

This shows the similarity between printing results to the console in Rust and Python. The above code in Python and Rust will display the result: My name is Mayowa. Another thing to note is the similarity in the type of the variable name. The variable name has the type of string declared as &str and str in Rust and Python respectively. This is another similarity between Rust and Python.

FUNCTIONS — Snake-Case Naming

Another similarity between Rust and Python is in the function name. Function name in Python by convention has always been the snake-case. Rust follows the same convention. Both Rust and Python follows the same convention for declaring function names.

def sample_function():
print("This is a sample function")
fn sample_function() {
println!("This is a sample function");
}

FOR-LOOPS

Python for many years have held the title for the simplest declaration of loops. A for-loop in Python is defined like this:

for i in range(1, 10):
print(i) // prints 1, 2, 3, 4, 5, 6, 7, 8, 9

A for-loop in Rust is defined like this:

for i in 1..10 {
println!("{}", i); // prints 1, 2, 3, 4, 5, 6, 7, 8, 9
}

Do you see the similarity?? As an experienced Python developer, I can say that I like the rust for-loop syntax. It’s cool.

RETURN TYPE

Since python provided support for typings, Python has provided a way for static type-checkers and linters to know the return value of a function. Here’s an example:

def main() -> int:
return 3

Rust uses the same syntax for returning value type.

fn main() -> i32 {
3
}

See the difference? Both languages use the same symbol -> for returning the function’s return value type.

TUPLE DATA TYPE

If you are conversant with Python, you will know of one of the data types that was unique to Python before the explosive popularity of Rust — the tuple data type. The tuple data type is an immutable data type with fixed length. A tuple can contain different data types. A tuple in Rust works similar to how it works in Python Here’s a tuple in Python and then in Rust.

names = ("Mayowa", "Obisesan", "?")
f_name, s_name, m_name = names

# OR

f_name, s_name, m_name = ("Mayowa", "Obisesan", "?")

The above code are all valid tuple definitions in Python.

Let’s see a similar representation in Rust.

let tup: (&str, f64, u8) = ("Mayowa", 1.3, 5);
let (f_name, y, z) = tup;

// OR

let (f_name, y, z) = ("Mayowa", 1.3, 5);

You can see the similarity in defining the tuple and destructuring the tuple from both languages. Tuple data type is another striking similarity between Python and Rust.

Conclusion

I just started learning Rust, but I have come to appreciate the Rust programming language. The language has borrowed many programming concepts from some of the most widely used languages, TypeScript, Python and C++ and found a way to make all these concepts work intrinsically well in the language. Rust embodies simplicity at its code and in its code syntax without losing efficiency, speed and power as a low-level language. Rust is truly a beautiful language. And for me, as a seasoned Python developer, learning Rust has been familiar. It is interesting to see that the major concepts like variables, loops and strings handling of the language is familiar to me a Python Programmer. This similarity has helped my learning Rust to be smoother than I expected. As I continue in my learning of Rust, I hope to find more interesting features that the language offers and write more about them.

Thanks for reading. 🙂

--

--

Mayowa Obisesan

Entrepreneur | Computer Scientist | Quantum Theory Enthusiast