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" may sound 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. Its memory safety, concurrency safety, and execution efficiency are unparalleled. 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 for the open-source community, these features make it more suitable for large-scale collaborative development based on version control systems (default in Rust Cargo is Git). Rust has gained considerable recognition after 9 years of development, even though it is a relatively new language. This includes many well-known companies and famous open-source developers (such as Linus Torvalds, the Linux kernel developer, who has started preparing to add Rust to the kernel to gradually replace some C). You may not understand any of these terms, 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 precisely because many of Rust's features are proposed to address the criticisms of C/C++ and other compiled and slightly closer-to-the-metal languages. And the difficulty of learning Rust is precisely because of this point - if someone who has never been exposed to low-level programming learns Rust, they will find that many features are completely unnecessary and nonsensical. In fact, this is because if you have never been exposed to programming, or have only been exposed to 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, the most important part of computer operation principles will be conveyed to readers in a concise and concise manner. 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, "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 its extreme ease of learning. 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. They can provide limited breadth and depth in terms of employment market and the status in the open-source world. Without understanding the working principles of modern computers and only focusing on handling business logic (which is the focus of JavaScript), once there are low-level problems, there is no way to troubleshoot them on your own and you will still rely on others. Low-level matters do not disappear just because a language is higher-level; on the contrary, someone always needs to handle low-level business, and that is the task of interpreters and interpreter developers; only 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; whether it is efficiency or flexibility, it is 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. And maybe you can become a master of handling low-level business and a leading figure in the industry.

In addition, Rust is not as many people say, "used to write drivers and operating systems." On the contrary, many companies today have applied Rust to 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. But 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 related 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 appear to be highly complex devices, and indeed they are. The structure inside an ordinary notebook is probably the highest level of integration that humans have achieved so far. However, regardless of this, a computer is a machine completely designed by humans. Scientists have considered designing computers as complex as the human brain in order to achieve similarly complex and clever functions. As early as the mid-20th century, a group of scientists attempted this, but limited by the then (and now) limited understanding of physiology, even the most general schematic diagram of the human brain is difficult to clarify. So, a group of scientists led by John von Neumann chose another path, which is to design a super simple computer architecture; after all, as long as the goal of creating a general-purpose computer is achieved, there is no need to pursue human brain-level complexity. 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 in the past, present, and future: the Central Processing Unit (CPU), the Input/Output (I/O) Bridge, and the Memory. Simply put, the CPU is responsible for calculations, the I/O Bridge is for input and output, and the memory is for storage. Don't make jokes, memory refers to RAM, also known as "memory modules". The part that is often confused is the hard disk; the hard disk is one of the input/output devices connected to the "Bus" (commonly known as the bus) in the chart above! Although both memory and hard disk can store things, the biggest difference is that the hard disk is used to accept the output results of the "factory" formed by the CPU and memory (otherwise, why is it called volatile storage), and provide input materials. The memory, on the other hand, is like the tables and chairs inside the factory, providing temporary storage space for workers and various tools, as well as a platform for processing materials. To avoid confusion, in this booklet, all "memory" will be referred to as "main memory", and "running data" will be used to refer to all data and instructions stored in volatile storage. At the same time, the factory analogy will be used throughout this booklet, and you will know the reason later.

There are still components inside the CPU that have storage capabilities. The universal mechanical principle determines 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, and at most they will not exceed 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 toolboxes, where small parts can be stored for a short time and are conveniently accessible. The time required to put things on the processing table (read from and write to the main memory) is much longer than the time required to take parts 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 to store various data in the cache, registers, or main memory-this should not be believed and practiced because modern programming languages and modern operating systems, for safety and stability reasons, will ignore such requests from programs. Rust, for example, does not give any program the right to choose the location of runtime data storage during compilation. 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 running data, and other I/O devices (including hard disks, printers, cameras, microphones, speakers, etc.). It's like the factory manager (operating system) telling the workers where everything should be placed; they can't just put things anywhere they want.

The principle of the CPU providing calculations is very complex. But everyone knows that it can perform basic arithmetic operations and a series of advanced functions, etc.; these are already in 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. And my abilities are limited, so I won't go into too much detail on this part.

The I/O Bridge, referred to as input/output or I/O bridge in this article, connects the I/O bus to 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, "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 (input/output). Every factory has an input port and an output port; each machine (program) can also have an input port and an output port. So if you see I/O used to describe user input and what is displayed on the screen when programming, don't be surprised.

So, our journey through the computer system ends here. This part of the content can be written into a masterpiece if explained in detail ("Computer Systems: A Programmer's Perspective" is recommended for further understanding of computer systems), but for the purpose of program-oriented learning, these are more than enough.

P.S. Why is the analogy of comparing computers to factories and programs to 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, they can transform into various specialized machines (and even transform simultaneously), and can be transformed into various specialized machines before the birth of general-purpose computers or their use in specific fields (generally called dedicated devices, ICs, etc.). For example, a switch is a typical dedicated computer, it is not programmable and cannot be used as anything else; but a personal computer is a general-purpose computer, you can turn it into a telephone, an email sender and receiver, a switch, a router, a server... through programming or installing existing programs. With the maturity of general-purpose computer technology and no longer needing to design dedicated integrated circuits like dedicated computers every time, and for the convenience of implementing more functions, more and more dedicated computer devices have been replaced by general-purpose computers. For example, IoT 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! This analogy can reasonably hold because it describes the world before the birth of general-purpose computers in a certain sense-also a world that most people are familiar with and instinctively accept.

In the next chapter, we will learn the highlight: 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.