Torch

Torch

Rust Programming Language Learning Guide Outline

Rust Programming Language Study Guidelines.

Introduction#

First and foremost, I know that the name of this "Rust Programming Language Study Guidelines" sounds a bit grandiose. But rest assured, the only reason for choosing this name is to make it sound impressive, so that when you carry this booklet to public places, you may (perhaps) attract a lot of attention and maybe even increase your enthusiasm for learning.

Rust was born in 2015 and has been around for 9 years. It is a programming language with a certain history. If you search for information about Rust on the Internet, you will see various opinions and comments saying that Rust is a language that is difficult to learn but has many other good qualities. Indeed, some people say that Rust has all the excellent qualities of other programming languages except for being difficult to learn. It has achieved unparalleled levels of memory safety, concurrency safety, and execution efficiency. In addition, with the efficient or modern Cargo package manager and a complete set of compilation tools that comply with the C standard (the official Rust compiler is still based on the gcc GNU C Compilers), Rust's fully static compilation mode makes development and debugging particularly simple. As a language from the open-source community and aimed at the open-source community, these features make it more suitable for large-scale collaborative development based on version control systems (in Rust Cargo, Git is the default). After 9 years of development, Rust, although a relatively new language, has gained considerable recognition, including many well-known companies and renowned open-source developers (such as Linus Torvalds, the Linux kernel developer, who has started preparing to gradually replace some C with Rust in the kernel). You may not understand any of the terms mentioned above, but rest assured, the main point summarized in this paragraph is: Rust is definitely a good language.

"But Rust is difficult to learn. Shouldn't I learn C first, as recommended online?" Indeed, if you have experience with C or C++, learning Rust will be much easier for you, and you can skip the first chapter of this booklet ("Understanding Computers"). However, the reason why there is a saying that learning C/C++ can help with learning Rust is because many of Rust's features are precisely designed to address the criticisms of C/C++ and other compiled and somewhat low-level languages. The difficulty of learning Rust is precisely because of this point. If someone who has never dealt with low-level programming learns Rust, they will find that many features are completely unnecessary and absurd. In fact, this is because if you have never dealt with programming or have only dealt with high-level programming (such as JavaScript, Python, or even Scratch), your understanding of computers is completely inadequate. This booklet will innovatively refuse to teach readers how to print "Hello World" right from the beginning (in fact, readers will soon realize that there is no need to write "Hello World" because the default template for a new Rust project is "Hello World"). Instead, through about 1000 words of illustrated text, it will concisely convey the most important parts of computer operation principles to readers. The subsequent chapters will also start from the principles. In this way, even if you have never learned any other programming languages, with a further understanding of computers, you can still appreciate Rust's excellent design and achieve better learning results.

You may still have questions like, "Why should I learn a language that I have never even heard of?" Indeed, Rust is avoided by many beginners because of its difficulty and relatively narrow application scenarios. They believe that languages like Python and JavaScript are completely sufficient for their daily needs and can even be used to make a living. Their statements are not wrong, but you need to realize that JavaScript is a programming language for the World Wide Web, and its biggest feature is that it is extremely easy to learn. If you have learned Rust, then JavaScript is almost unnecessary for you to learn. There is no intention to belittle any of these scripting languages, but the fact is that anyone can learn and master these higher-level languages (when we say "higher-level" and "lower-level," we are referring to the distance from the hardware), and they are highly innovative and highly dependent on frameworks. The breadth and depth of their positions in the job market and the open-source world are very limited. Without understanding the working principles of modern computers and only focusing on handling business logic (which is the focus of JavaScript), once there is a low-level problem, there is no way to troubleshoot it on your own and you will still rely on others. Low-level matters do not disappear just because a language is high-level; on the contrary, someone still needs to handle low-level business, and that is the task of interpreters and interpreter developers. They can become the center of the entire industry. Some low-level features, such as pointers, are just wrapped and managed by interpreters like nannies; in terms of efficiency and flexibility, they are definitely not as good as doing it yourself. If by learning Rust, you can have a more rational and accurate understanding of computers, then you will be able to solve many problems encountered in life and production with a clear mind, and learning other languages will also be easier. Perhaps you will become a master of handling low-level business and a leading figure in the industry.

In addition, Rust is not just for "writing drivers and operating systems," as many people say. On the contrary, many companies today use Rust in production environments to run their network servers and other digital infrastructures. This is because Rust has two important features: high performance brought by zero-cost abstractions and operational safety brought by modern memory and concurrency management. However, if you do not understand the basic principles of computers and still treat the interpreter of a scripting language as your nanny, then these two factors that are crucial to the stability of the entire computer era will never be understood.

Understanding Computers#

Prerequisite knowledge for this chapter: basic life experience. If you encounter any unfamiliar terms and no corresponding explanations are given in the text, please skip them directly; these terms are not helpful or necessary for understanding this chapter and this booklet (for now).

Modern computers may seem like highly complex devices, and indeed they are. The structure inside an ordinary notebook is probably the highest level of integration achieved by humans so far. However, regardless of how complex they may seem, computers are machines that are completely designed by humans. Scientists have considered designing computers to be as complex as the human brain in order to achieve similarly complex and ingenious functions. As early as the mid-20th century, a group of scientists attempted this, but limited by the physiological understanding of that time (and even now), it was difficult to clearly depict even the most general schematic of the human brain. Therefore, a group of scientists led by John von Neumann chose another path, which was to design an extremely simple computer architecture; after all, as long as the goal of creating a general-purpose computer can be achieved, there is no need to pursue the complexity of the human brain. Thus, the "super simple" von Neumann architecture was born.

Mermaid Loading...

Well, maybe it's not "that simple." But the outline is clear. The von Neumann architecture defines the basic architecture of all general-purpose computers, past, present, and future: the Central Processing Unit (CPU), the Input/Output (I/O) Bridge, and the Memory. Simply put, the CPU is the part responsible for calculations, the I/O Bridge is literally what it sounds like, and the Memory is the place responsible for storing things. Don't make a joke, the Memory is RAM, also known as "memory modules". The often confused component is the hard drive; the hard drive is one of the input/output devices connected to the "Bus" (commonly referred to as the bus) in the diagram above! Although both the memory and the hard drive can store things, the biggest difference is that the hard drive is used to accept the output of the "factory" formed by the CPU and memory and provide input materials, like a warehouse for the factory; while the memory is the desk and chair inside the factory, providing temporary storage space for workers and various tools, as well as a platform for processing materials. To avoid confusion, this booklet will use "main memory" to express all instances of "memory," and "runtime data" will be used to refer to all data and instructions stored in volatile memory, please note. At the same time, the factory analogy will be used throughout this booklet, and you will understand the reason later.

There are still components within the CPU that have storage capabilities. The universal mechanical principles determine a characteristic of storage devices: the smaller the capacity, the faster the speed (in the context of ignoring technological differences). Therefore, these storage components inside the CPU have very small capacities, at most a few megabytes (of course, this size is already provided by modern CPUs; some older textbooks may say that the size of the storage components in the CPU is only a few bytes). Still using the factory analogy, if the main memory is the processing table where materials to be processed can be placed, then these storage components inside the CPU are the toolboxes, where small parts can be stored for a short period of time and are conveniently located for quick access. The time required to place something on the processing table (read from and write to the main memory) is much longer than the time required to take a small part from the toolbox (read from and write to the storage components inside the CPU), even though the former is less than half a millisecond.

These storage components inside the CPU have their own names. Some are called "cache," and some are called "registers," but readers do not need to know them. Some textbooks may say that when programming, you can request that various data be stored in the cache, registers, or main memory—please do not believe or practice this because modern programming languages and operating systems, for safety and stability reasons, will ignore such requests from programs. Rust, during compilation, does not give any program the right to choose the runtime data storage location. Therefore, we will not continue to introduce and understand the storage components inside the CPU; readers only need to know that they exist. For programs, the only parts they can see are the CPU that provides calculations, the main memory that provides storage for runtime data, and other I/O devices (including hard drives, printers, cameras, microphones, speakers, etc.). It's like the factory manager (the operating system) telling the workers where everything should be placed; they can't just put things anywhere they want.

The principles behind the CPU's calculations are very complex. But everyone knows that it can perform basic arithmetic operations and a series of advanced functions, etc.; these fall into the realm of mathematics and digital circuit design, and although we want to get close to the hardware, we don't need to get that close. Moreover, the author's abilities are limited, so this part will not be elaborated on.

The I/O Bridge, referred to as input/output or I/O bridge in this article, connects the I/O bus with the CPU and main memory, just like the material input and product output of a factory. Without the I/O bridge, the existence of this factory would be meaningless. The term I/O bus may sound a bit strange, but its English name explains everything very well—it's called a "Bus," like a bus. Indeed, the I/O bus is like a bus, carrying data from their places (which can be many possibilities, such as the output of another factory or a dedicated device) to the factory, or vice versa. The term I/O is also widely used because it means input/output. Every factory has an input port and an output port; similarly, each machine (program) can also have input and output ports. So if you see I/O used to describe user input and what is displayed on the screen when programming, don't be surprised.

And so, our journey through the computer system ends here. This part of the content can be written into a masterpiece if explained in detail (I recommend reading "Computer Systems: A Programmer's Perspective" if you want to deeply understand computer systems), but for the purpose of programming learning, this is more than enough.

P.S. Why is the analogy of computers as factories and programs as machines so common and why does this article insist on using it? This is because the full name of modern computers is actually called general-purpose computers. In other words, like Sun Wukong (the Monkey King) who can transform into 72 different forms (and even simultaneously), they can transform into machines for various specific purposes—actually, before the birth of general-purpose computers or their use in specific fields, there were specialized computers (generally called dedicated devices, integrated circuits, etc.). For example, switches are typical specialized computers; they are not programmable and cannot be used as anything else; but personal computers are general-purpose computers, and you can turn them into telephones, email senders and receivers, switches, routers, servers, etc., through programming or installing existing programs... With the maturity of general-purpose computer technology and no longer needing to design specialized integrated circuits like specialized computers every time, and for the convenience of implementing more functions, more and more specialized computer devices have been replaced by general-purpose computers. For example, Internet of Things devices such as networked water heaters are controlled by a general-purpose computer (microcontroller) on a single circuit board; in a sense, these devices are essentially the same as personal computers! Such an analogy can reasonably stand because it describes a world before the birth of general-purpose computers—a world that most people are familiar with and instinctively accept.

In the next chapter, we will learn the highlight of the program: writing Hello World in Rust. (But actually, we will write a simple string search program—don't be nervous, it's easy to do.)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.