RUST on programming languages

Nishant Joshi
Dev Genius
Published in
5 min readJul 21, 2020

--

Random meme, before geeking out

The title is a bit confusing to understand the context of the content. In this blog, I am going to run around and see the different aspects of programming language rust. And talk about the concepts that it introduces that are useful for various aspects of programming.

What is Rust?

Simply putting it is a statically as well as strongly typed programming language.

Let me explain:
statically typed indicates that all the datatypes that are expressed in the code are known at compile time and memory allocation is done properly.

Then what is 👆 that? Let’s just say rust knows what you want to say.
But this doesn’t mean you could declare variables for a complex data type and expect rust to understand. Here comes the next point I mentioned above.

strongly typed indicates that the types are designed to make it harder to write syntatically incorrect code.

If you were to do even a little mistake with the syntax or definition of variables then the errors are caught at compile time. Not just the syntax errors but there are various tests build in the compiler to check for unused variables, dead code(Code that will never run), infinite loops as well as the lifetime of variables.

What makes it different?

This is the question that puzzles people before learning any new language if it were a verbal language or programming language.

  • As I was talking about the language I said there are various tests build in the compiler which makes it harder to make mistakes, while writing the code, what this means that there is no stone (memory) untouched while running the code, all the memory accesses are checked.
  • This feature is derived for the one above, due to this checks the machine code that is generated after compilation a pretty clean, as well as highly stable due to strict compilation policies.
  • A new concept borrowing is introduced, it is similar to call by reference to that of C. But due to the variable handling that is implemented in the language, once a variable is passed to another function the scope of the variable ends in the current functions, and the memory is freed when the function exits. Borrowing helps function to use variables but instead of freeing them, they are returned back to the caller function.
  • The binary that is compiled is statically linked and packs the rust runtime and does not perform dynamic linking, other than that it obviously has all the basic safety implementation which are implemented for C like PIE, stack smashing protection, NX, etc.
  • There is code obfuscation enabled which makes the assembly code hard to understand.
  • Keeping the security aside, as the language generates clean machine code, it runs very fast, a speed comparable to that C++.

Enough with the nerdy stuff! What does it provide with these features anyway?

Due to the features for generating clean machine code, and proper memory management and protection techniques. It makes a perfect language for developing components of an operating system, device drivers, self-contained application, command-line application, systems for embedded devices, and even for web-assembly code.

  • operating system: taking into consideration the Linux operating system most of its code is written in C. Thus it would be easy to construct modules for Linux using rust, and the rust and direct access to the system call. Due to the stability of the generated code, the modules would be much more efficient. It could be possible maybe to transcribe the entire Linux kernel into rust. (Well who know, let’s hope for the best).
  • Similar to the open above writing device drivers in rust makes them much more secure.
  • self-contained applications and command-line applications are simple examples of programs that can be used in either windows or Linux.
  • As rust compiles to a pretty low-level code it would be much easier to program embedded devices, due to the high-level functionality present in rust.
  • web-assembly is a fairly new concept where we use precompiled applications through a web browser to increase the performance of the running web application on the client-side.
  • Besides this building services would be much easier and these services would be more predictable and manageable with higher security.

Looking at these practical usages, it feels like the applications for this language are limitless. But due to the strong typing of the language, it is difficult to perform maneuvers that can be done in C or even C++. Some other functionality that is present in rust is the proper multi-thread handling and even this is strongly typed, which makes the code invulnerable against race condition, illegal memory allocation, etc.

Language to Programmer

Let’s talk about this language from a programmer’s perspective.

  • Being a strongly and statically typed language you might think that the language would be tedious to write and every aspect of it should be specified manually. Luckily, worry not, there are various features that make if fairly simple to write this language, datatype guessing while variable declaration, then the errors, warnings generated are very descriptive so it is very easy to debug the code.
  • Though this is a functional language (similar to C). It as structures, enumerators similar to that of C. But these structures can have their implementations. It would be like “What methods are to class, implementations are to structures”, I am just paraphrasing it.
  • It has tuples that are similar to structures but they don’t have variable names of their contents, they are mainly used to organize values of different datatype under a single variable.
  • For making development easier rust has cargo. Cargo Is a build system designed for rust as well as a rust package manager. Cargo takes care of dependencies. The only this developer has to do is specify the name of the dependencies and they can be included in your project using theuse keyword.

Now, reading through this blog it feels like there was no coding involved, but this blog wasn’t meant to be about programming. It was an insight into a language new to this decade, but fast and impactful enough to be compared with C, in terms of its application. There are various sources to learn this language, not by me but they are available on the official site: rust-lang.org.

If you are reading this then you didn’t leave halfway, which makes me think the content was, unto the standards of the people who are reading it. Thank you….

--

--