origin

Problems with existing languages

Many languages still struggle with both memory safety and thread safety.

Rust appear

In 2006 Graydon Hoare set out to develop a new programming language called Rust. The name Rust is derived from Rust, which also means Rust, and the words “Trust” and “Robust” are softened.

GH’s expectations for language

  1. It must be more secure and crash resistant, especially when working with memory.
  2. You don’t need a system like garbage collection to introduce a performance burden for memory safety.
  3. Rather than a language with just one major feature, it should have a broad set of features, with some consistency between them. These features work well together to make love languages easier to write, maintain, and debug, allowing programmers to write safer and more efficient code.

Rust should be a modern system-level programming language that pursues security, concurrency, and performance at the same time

The design philosophy

  • Memory safety
  • Zero-cost abstraction
  • practical

Memory safety

The first thing Rust does to ensure memory safety is to ensure type safety. Type safety has the following benefits: – Allows the compiler to detect meaningless or even invalid code, exposing hidden errors in the program. – Provides the editor with meaningful type information to help optimize code. – Improves code readability and makes the developer’s intentions more explicit. – Provides a level of high-level abstraction to improve development efficiency.

What is Memory Security

In short, there will be no memory access errors. When the program access undefined memory memory error is produced, generally there will be a focus: – references to null pointer – the use of an uninitialized memory – release after use, also is to use dangling pointer – buffer overflow, such as an array – illegal release is a pointer to the pointer or undistributed pass, namely repeated release

Rust and memory management model
- Ownership system. Each allocated memory has a pointer to exclusive period ownership. Only when the current pointer is destroyed can its corresponding memory be freed. - Borrowing and life cycle. Each variable has a life cycle, and once the life cycle is exceeded, the variable is released automatically. If there is borrowing, you can prevent dangling Pointers, which are used after release, by marking life cycle parameters for the compiler to check.Copy the code
Rust borrows features from Haskell
- No null Pointers - Default immutable - Expressions - Higher-order functions - algebraic data types - Pattern matching - Generics - Traits and association types - Local type derivationCopy the code
To achieve memory security, Rust provides the following unique features
- Affine Type, which is used to express the Move semantics of Rust ownership - borrowing, life cycle.Copy the code

Zero-cost abstraction

The cornerstones of zero-cost abstractions in Rust are generics and traits.

practical

How to evaluate the usefulness of a programming language
- Practical, must first be able to be applied to the development of industrial products, and secondly be easy to learn and use. - Beneficial refers to the ability to have a positive effect or influence on the industry. - Stability refers to the stability of the language itself. There are no random results for different users when solving the same problem.Copy the code
Rust performance
  1. practical

    • In the case of failure, an assertion tool can be used.
    • For errors, Rust provides layered error handling based on return values, such as Option for cases where control is possible, and Result specifically for errors that can be reasonably resolved and need to be propagated.
    • Rust treats exceptions as a problem that cannot be reasonably resolved and provides a thread panic mechanism that can safely exit when an exception occurs.
  2. Benefits and stability

The Rust language can greatly improve the quality of software by solving the problems of internal security and concurrency security. Rust offers a better alternative to C and C++ because Rust is a security, concurrency, and performance focused language that can be used for embedded systems, operating systems, web services, and other low-level systems, as well as web application development, game engines, and collective learning. Webassembly can be used to develop front-end components. It can also be used for blockchain technology.