Some Rust Learning Experience (pt. 1)

AmmarAmmar
5 min read

Starting with Python as the first programming language is good. The language is easy to use, focus on the main fundamental of programming, abstracting certain part of computer science components as they handled it on background; which make the beginners thought that coding is really easy.

However, due to this setting, Python learning process has removed certain important concept that is quite useful for beginners that I found out in - Rust.

My objective of learning Rust is actually simple. To learn a static typing programming language and taste the memory safety conceptual in coding (which actually I need to learn by enrolling computer science course). I am not sure that I might build something with Rust in the future or what people said just to feel, but I hope I can bring some paradigm that I learn in Rust during writing with Python in my daily task.

The first thing that I found giving some culture shock is the static typing itself. In Python, sometimes we never bother the parameter type hint and return type; due the nature of non-enforcement type hinting in the code. Type hinting is just to help the programmer in developing the project as it will pop-up associated method for the particular type of value in the VSCode. However in Rust, type hint, or what they called types are enforced and also being used by the compiler to do something. For example for casting (or converting) data type in Rust, the type that we put will be used by the compiler so it can know what kind of data type that we want to convert to.

Not only that, I observed that, by enforcing types, the code is much robust, readable and easy for hand-over process in work wise. Just to say that if you felt overwhelmed seeing this kind of heavy type include code, it just a skill issue - no, just kidding, it just you don't know like me and let's learn together. Having types make the code easier to read by other programmers, as they know what kind of types is the parameter, what kind of types return by the function. Simple types that does not existed in Python which is u32 or unsigned 32-bytes integer, which referred to no sign (negative) integer which means, the number will always positive. It is small but somehow cool.

Rust compiler also doing an amazing job of checking the consistency of types and return a helpful stack-trace message for the user so we can check the error part. Another cool stuff of Rust, its compiler stack trace.

Besides, the mutability and ownership concept. In Python, at least in my experience of using Notebook, there is always a situation of what I called as state-conflict where I have no idea what value the variable is actually hold on too and conflicting with another code block; due to running multiple code block in no order, and changing the variable value on another code block, while sharing the same state. With mutability by default in Rust, all variable is immutable unless the mut syntax is implemented. There is no such thing in Python of having bother about mutability where ironically some of the compound data type itself can be manipulated even though it has been stated as immutable (tuple in Python) where there is work around to make it mutable. As per stated by default variable is immutable in Rust, we need to explicitly include mut if we want to enforce the mutability and also consider whether it is growable or not. For example, str is a string that is not growable (cannot add new char in it), while String is growable. Which bring to the next concept which is ownership.

Ownership stated that, a variable always an owner, every value has a single owner, owner will dropped once it is out of scope. A simple objective of implementing ownership is to somehow to avoid "copy"-ing value in order to utilize the memory, by deallocate it once it is out of scope. To be honest, it took me a times to understand this concept, because it is newly introduced to me (which no one care passing value and about memory allocation in Python) and it's quite daunting whenever it comes to struct and implementing the borrowing, as it is proned to error.

And because of the static typing paradigm (where it feels like living in a prison where everything is being observed), your "freedom of not including the type" is also required, within the Generic chapter. What I meant here is, there is a point where we don't want to explicitly put concrete types (i32, String) but we just want to make it general. Hence, to make it general, we need to explicitly tell Rust that we want to do this by using T type. It really feels like in concentration camps, but I kind like about this verbosity.

Rust also has OOP such Python that used struct and their own abstract base classes (ABC) that worked as contract by using impl. To compare both of this language almost has the same feature, however the thing that I loved in Rust is in the module part. In this part, we can explicitly declare the module to become public and private to be access by user by using the syntax pub. I know there is such thing in Python, where it used dunder method (underscore) to make it private and protected, but it seems kind implicit comparing to putting the syntax within the code explicitly. We can exactly see which part of the module, struct, method that can be access and not, making it much easier (I think) in building an the crate and import wise.(I am avoid on talking about Python unresolved parents/relative module import issue that has various kind of answers in Stack Overflow discussion).

If you are a C or Cpp user, I am apologize because all of this things has existed in your language. But I found that Rust has cool documentation and fantastic lesson written so I took the opportunity to learn from it. So far, this is what I felt while learning Rust. It somehow gave me some another point of view of coding that I might bring in my Python style (or if I do build with Rust later on). I still learning (if you found any of the facts are wrong or inaccurate, please do comment so I can correct it), and currently on chapter 10.

I might update this articles later or create new pages for the next updates.

0
Subscribe to my newsletter

Read articles from Ammar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ammar
Ammar

Long life learner.