Rust And Python: The Tradeoffs

Shashi Kant
5 min readJul 13, 2023

--

Python has been one of the go to languages for backend development and data science for the last couple of decades because of its simplicity and high speed of developments whereas Rust is quite new in comparison of Python and is a system programming language which has been designed for performance critical tasks but in this era of nano services, it is gaining popularity among the backend developers and data scientists to create either the whole applications using Rust or creating some services dealing with performance critical tasks. All the languages have their own merits and demerits so these too come with their own. In this blog, we will try to find the important trade offs between the languages and will know where their strengths lie.

Python Image(source: pexels.com)

Productivity And Performance

The strongest power of Python lies in its simplicity while that of Rust lies in its performance. Although these are extreme statements but still give us good idea that Python is easy to learn and write programs while Rust is not that easy but gives us performance benefits. Let’s try to understand both the languages on the terms of productivity and performance.

Python being a high level language, handles references and actual copies of variables by itself, uses garbage collector to clear out the unused variables for faster performance as well as complete prohibition of memory leaks and segmentation faults. In the addition Python being dynamically typed language, does not require the types of variables to be defined in the code by the programmers and it even extends its simplicity to such level that developers do not even need to think about where they are initializing their variables, they are catching errors or not or whether the variable is actually a constant. Although Python provides us object oriented programming but all the members of class are public(Although we use _ convention to mark it as private but still that’s just a convention). All of these luxuries come at a cost, the cost of performance. Python provides this level of simplicity to developers but it has to monitor a number of things like all the references, memory allocations for variables which can be initialized at any time, memory deallocations for unused variables, change of allocated memory size for variable as its type changes and still keep itself safe and free from memory leaks and segmentation faults. In this way, Python provides high productivity through its simplicity but that comes at the cost of performance.

Rust is a low level statically typed language designed for the performance critical tasks but still being free from segmentation faults and memory leaks. In Rust, the data types of all the variables must be known at the compile time and all the variables are immutable until specifically declared mutable. Rust forces developers to handle all the None cases and Errors, even it needs to know, at the compile time, which kind of errors will be raised at which point of program and how will it be handled. Every method, struct or its field is private until and unless specifically written. In this way, Rust forces the developers to implement all the scenarios which can raise because of the datatypes of variables defined by the developers and makes its own work easier at runtime as it is damn sure about its variables, allocated sizes, time of memory deallocations, control flow etc. which not only results in faster and high performance work but also safe from faults like segmentation faults and memory leaks.

Rust Image (Source: pexels.com)

Concurrency And Parallelism

Concurrency and parallelism are two most important pillars of modern day software applications and both the languages support these but the level of the concurrency and parallelism is limited. Rust has an excellent support of concurrency and parallelism through its reference model and explicit definition of mutable and immutable variables. In this case too, the simplicity of Python comes to make a clear dent on its concurrency and parallelism because it makes Python to use Global Interpreter Lock(GIL) to be memory safe which stops it utilize the maximum power of available resources.

Programming Paradigms

Python being the most simple programming language supports both programming paradigms, functional and object oriented but to a limited extent. While main constraints of functional programming paradigm are immutability, pure functions and declarative style, Python does not enforces any of the above constraints and it is up to the developer whether to mutate the variables and whether functions will use external details as well as it doesn’t even enforce us to declare the input and output datatypes of the function. When it comes to the object oriented programming paradigm, Python is rich of features like encapsulation, inheritance and composition but still it misses the access modifiers which define clear boundary lines among different classes and it does not encourage interfaces to be clearly defined but still we can achieve that by using abc module. In this way Python, tends more towards object oriented programming but few weak conditions like lack of access modifiers etc.

Rust is mainly functional programming language which encourages or even enforces to some extent to use the immutability, pure functions and declarative style. Although there is a concept of structs where we can define specific datatypes as well as we can implement methods to the structs which increases the encapsulation but there is no concept like inheritance. So there is only one option left to define the relation among objects is through composition.

Conclusion

Both the languages have their own merits and demerits. While Python provides simple and productive environment on the cost of performance, Rust does the vice versa. Python provides us both object oriented programming and functional programming but tends towards object oriented programming while Rust mainly focuses on functional programming but provides a way of encapsulation and composition through the use of structs. Both of these are standing just opposite to each other and developers can take the advantage of both the languages by thinking them as complementary to each other. For the sake of productivity, Python is really very good language but we can program the performance critical tasks in Rust and create Python bindings of those functions using Foreign Function Interface(FFI) and can use them in Python code. In this way, we can leverage the productivity of Python with the high performance of Rust to achieve an easily scalable software with high performance.

--

--

Shashi Kant

Backend developer with professional experience in Python, TypeScript and Rust.