What is the programming language "Rust"? Hold the basics wit
Mozilla-backed open source programming language "Rust"
Rust is a new programming language, development began in 2006, and the first stable version (version 1) appeared in 2015. From now on, let's start with the overview. Rust is an open-source programming language supported by Mozilla, which develops the web browser software "Firefox". When the development started in 2006, it was a personal project of Mr. Graydon Hoare who belongs to Mozilla, but from 2009 Mozilla itself joined the support and it became an official project. The relationship between Mozilla and Rust is that the web rendering engine "Servo", which Mozilla started developing in 2012, uses Rust.
Rust has undergone repeated specification changes, and version 1 was released in 2015. Since then, it has had a 6-week release cycle with a focus on backward compatibility. Currently, it is being developed mainly by community-based Rust Project Developers, and all files are published on GitHub.
Adopted by Microsoft and Google
Since November 2019, Microsoft has announced that it has adopted Rust for the development of its core platform, Windows. And in April 2021, Google announced that it would adopt Rust to develop its core platform, the Android OS. In addition, the move to use Rust for the development of the Linux kernel has begun.
The market price for the development of the core part of the OS was decided with C language and C ++ language, but Rust decided to make a hole there. Behind this is the solution to the security problems (vulnerabilities) of the OS. Many of the security vulnerabilities are attributed to memory usage bugs, which have been difficult to overcome due to the characteristics of these languages as long as C / C ++ is used.
Rust uses a unique language design to ensure memory safety (the security of memory usage is guaranteed by the language). Microsoft and Google value memory safety first and are starting to adopt Rust.
A language for system programming that replaces C / C ++
In this way, Rust has become an effective alternative to C / C ++ in situations where C / C ++ has been or will be used.
The first reason is that Rust is a programming language that has the compactness and high speed of a native compiler language, and allows fine handling of resources such as memory. However, simply being lightweight, fast, and capable of fine-grained resource manipulation does not make sense to replace C / C ++.
Rust overcomes the weaknesses of C / C ++, such as low parallelism and the dangers represented by pointers (* 1). In other words, Rust is a programming language that ensures parallelism and enables detailed resource operations while maintaining high security.
* 1 Rust also includes a language specification called Unsafe Rust that emphasizes efficiency at the expense of memory safety. By using this, you can operate memory like C / C ++, but I will not touch on it in this series.
The mechanism for ensuring memory safety
Due to the old design of C / C ++ (C is about to be 50 years old), efficiency is prioritized and safety is secondary in order to maximize the capacity of CPU and memory at that time. There is a story that seems to have been done.
Pointers are easy to understand, and while enabling efficient memory manipulation with pointers, uninitialized pointers that do not point to valid objects, " dangling pointers ", and reserved memory remain unreleased. It has caused problems such as causing program defects due to " memory leaks " that cause memory resource depletion.
In the native compiler language, dynamically checking the validity of pointers is a trade-off with performance and is not generally done. Intermediate languages such as Java and C # perform dynamic checks, but they are disadvantageous in terms of performance.
Rust has a borrow checker to ensure memory safety at compile time. A borrow checker is a mechanism that statically analyzes the owner of a resource such as memory and the lifetime of the resource. In Rust, by setting a one-to-one constraint between resources and owners, if one variable owns an object, that object cannot be owned by another, and the lifetime of the disappearance of the variable and the destruction of the object. Can be managed.
Since the lifetime can be managed, the compiler can know when to destroy an object, and it can be destroyed immediately when it is no longer needed. This means that there is no need for a GC (garbage collector), and it is free from the GC-specific problem of "programmers do not know when to destroy objects and cannot control them."
Compact and fast compiled language
As you can see, Rust is a programming language with memory safety, but what about Java and C #, which also have memory safety, these languages are intermediate language types that require an interpreter, and they are the core part of the OS. Not suitable for system programming such as development. System programming requires a native compiler language that can be translated into machine language that can be directly interpreted by the target CPU.
Like Clang, Rust's compiler conforms to a compilation mechanism based on a virtual compiler platform called LLVM (* 2), and after being converted to LLVM's intermediate code, it finally runs on the target CPU. Generate a possible binary. Unlike the Java Virtual Machine, Java interprets intermediate languages at run time, while Rust is already a binary that can be directly executed by the CPU at run time.
These make Rust's binaries compact, run at speeds comparable to or faster than C, making them a good programming language for system programming.
* 2 Previously, it was an abbreviation for Low-Level Virtual Machine, but now it is simply (not an acronym) LLVM.
Support for multithreaded programming
C / C ++ is often problematic because it is not thread-safe (because there was no concept of threads at design time). Some standard library functions in C use static variables, so calling the function from multiple threads can cause contention. There is also the issue of deadlocks, where even if you lock variables to avoid conflict issues, then there are mutual unlock waits.
As of 2021, the situation has changed with the advent of POSIX-compliant multithreaded libraries such as threads, but the situation of having to rely on external libraries remains the same. Rust guarantees thread safety because the standard library provides multithreading capabilities.
Table 1 compares the programming languages and Rust that have appeared so far in terms of major items.
Main items Rust C / C ++ Java / C #
Native compilation ○ ○ ×
Memory safety ○ × ○
Thread-safe ○ △ ○
Garbage collector Unnecessary ― necessary
Table 1 Comparison of Rust with C / C ++ and Java / C #
Automatic test function
Broadly speaking, there are unit tests that test on a function-by-function basis and integration tests that test the entire application. Rust provides both of these automated testing features as standard.
In the case of Java, the external library "JUnit" for testing is famous, but in Rust, there is no need to install a separate test library, and the procedure is easy to unify.
Module System and Package Manager Cargo
Rust comes standard with a modular system. A modular system is a system that manages projects in a hierarchical and divided manner. A program with a single source file is fine, but when there are many source files and many programmers participate in the project, it is necessary to manage the project by dividing it by function or role.
Rust allows you to manage your projects in layers such as packages, crates, and modules. There is a mechanism in the language specification to use these, so no special external tools are required. You can manage these properly with a package manager called Cargo.
In addition to this, there are various features such as being a multi-paradigm programming language.