Welcome to the rust programming language, an introductory book about rust. The rust language will help you write faster and better
Trusted software. In the design of programming language, the readability of high-level language and the ease of control of low-level language are often a contradiction. Rust
Will try to resolve the conflict. By striking a balance between strong technical capabilities and development friendliness, rust will let you avoid some common pitfalls when writing and controlling low-level details (such as memory related)
Who is rust for
Rust has many reasons for many people. Let’s look at some of the most important groups.
Rust is a creative tool designed to collaborate in a large team of developers with various system capability levels. Low level languages can easily cause some subtle bugs. In other languages, these bugs can only be found through extensive testing and code inspection by experienced developers. In rust, the compiler plays the role of “gatekeeper”. When there are these elusive bugs in the code, including concurrent bugs, the compiler will refuse to compile. With the help of the compiler, the team can spend more time programming logic rather than tracking bugs.
Rust also brings some modern developer tools to system programming:
- Cargo, the built-in dependency manager and build tool make it very convenient to add, compile and manage dependencies, and can be consistent with the rust ecology.
- Rustfmt ensures the consistency of code styles of different developers
- The rust language server enhances the prompt of the integrated development environment (IDE) for code compilation and error information.
Through these and other tools in the rust ecosystem, developers can greatly improve their ability to develop system level code.
Rust is suitable for students and people who are interested in system programming. Through rust, many people learned some knowledge about operating system programming. The rust community is very welcome and happy to answer students’ questions. The rust team hopes to help more people understand the concept of system programming through a variety of ways, such as this book, especially those who have just learned programming.
Thousands of companies, large or small, use rust to achieve different tasks in the production environment. Including command line tools, web services, development tools, audio and video analysis and decoding, cryptocurrency, biology, search engines, Internet of things applications, machine learning, and even the main components of Firefox viewer.
Open source developer
Rust is used to serve those who use rust to build rust programming languages, communities, development tools, and third-party libraries. We welcome your contribution to the rust language.
People who value efficiency and stability
Rust is suitable for those who value the efficiency and stability of programming languages. The so-called speed refers to the speed at which you can create programs with rust and the speed at which you write rust. The detection of the rust compiler ensures stability through additional features and refactoring. This is in sharp contrast to the fragile legacy code in those languages that do not have these checks and make programmers afraid to modify. Through zero cost abstraction and high-level features, rust compiles low-level code as fast as manually written code. Rust ensures that the code is safe and fast enough.
Rust hopes to support other users at the same time. The users mentioned above are only the groups that benefit the most. Overall, rust’s biggest goal is to eliminate the tradeoff between security, performance and economy that programmers have had for decades. Welcome to rust to see if its choice is right for you.
Who is suitable for reading this book
This book assumes that you have used other programming languages, but does not assume a specific one. We try to make the material of this book as rich as possible so that people from any programming background can understand them. We won’t be likeWhat is programming，How to consider programmingToo much time is spent on this problem. If you are new to programming, you’d better read a book that introduces programming in detail.
How to use this book
In general, this book assumes that you will read the book from beginning to end in order. The later chapters are based on the concepts in the previous chapters, while the earlier chapters will not go too deep into a topic; We tend to review a topic in later chapters.
There are two types of chapters in this book: conceptual chapters and project chapters. In the conceptual chapter, you will learn about one aspect of rust. In the project chapter, we use what we have learned to build a small program together. Chapters 2, 12 and 20 are project chapters, and others are conceptual chapters.
The first chapter explains how to install rust, how to write a hello world program, and how to use cargo rust’s package manager and build tools. The second chapter is a practical guide for rust, which contains some high-level concepts. These concepts will provide more detailed information in later chapters. If you want to do it right away, the second chapter is a better breakthrough. At the beginning, you may want to skip Chapter 3. This chapter mainly covers the similar features of rust and other programming languages. At the same time, you will go directly to Chapter 4, which is about the ownership system of rust. However, if you are an OCD patient and are used to learning all the detailed knowledge points before learning the next chapter, you can skip Chapter 2 and go directly to Chapter 3, and then return to Chapter 2 when developing a project and using a specific knowledge point.
Chapter 5 discusses structures and methods, and Chapter 6 covers enumeration,
if letProcess control.
You can then define your own rust type using structs and enumerations.
In Chapter 7, you will learn about rust’s modular system and the privacy rules for organizing your code and public application interfaces. Chapter 8 discusses some common data sets provided by some standard libraries, such as vectors, strings and hash tables. Chapter 9 will discuss rust’s error handling concepts and skills.
Chapter 10 will delve into stereotypes, features, and lifecycles that enable you to build code that can be applied to many types. Chapter 11 is all about testing. Even with rust type assurance, testing is still necessary to ensure that the logic of your program is correct. In Chapter 12, we will build a tool for searching text in files, which is
grepA child of the command line tool. To achieve this goal, we will use many of the contents and concepts discussed in the previous chapters.
Chapter 13 will explore closures and iterations — two features that rust inherited from functional programming. In Chapter 14, we will learn more about cargo and discuss best practices for sharing your code base with others. Chapter 15 mainly discusses the intelligent pointer provided by the standard library and the characteristics of realizing its functions.
In Chapter 16, we look at different models of concurrent programming and discuss how rust can help you program risk-free in multithreading. Chapter 17 will focus on the differences between some programming habits of rust and some rules of object-oriented programming you are already familiar with.
Chapter 18 is about pattern matching. In rust, pattern is a powerful way to express inspiration. The nineteenth is a hodgepodge that includes many interesting, such as unsafe rust, macros, and life cycles, features, types, functions, closures, etc.
In Chapter 20, we will complete a web server that implements low-level multithreading.
Finally, the appendix contains some useful information about the rust language by reference. Appendix A contains the keywords of rust, Appendix B contains the operators and symbols of rust, Appendix C contains the feature derivation provided by the standard library, Appendix D contains some useful developer tools, and Appendix E explains the version information of rust.
You can read this book in any order. If you want to skip something, skip it directly. Just look back at the earlier chapters when you encounter some doubts. You can do whatever you want.
One of the most important parts of learning rust is learning how to read the error messages displayed by the compiler: these will guide you to complete the code that can run. Therefore, we will provide a lot of sample code that does not pass compilation, and the compiler will display error information in various scenarios. Remember, when you use an example, it is likely to fail to compile! Make sure you read the surrounding text to confirm that the example you are using is wrong. Ferris will help you distinguish the meaning of code that doesn’t work.
In most scenarios, we will guide you to correct the code that does not pass the compilation to the code that passes the compilation.
The source code used in this book can be obtained hereGithub