Rust programming basics: from the perspective of a master programmer

153
code

Rust is a new programming language that has obtained a lot of attention recently. For the seventh year in a row, the Stack Overflow developer survey showed a growing interest in this language. Rust enables you to develop highly efficient and secure software, and this language is regularly named one of the most loved languages among developers.

In this article, we will look at the peculiarities of this language, talk about the memory model and the data collection to deeply explore the Rust programming language.


Rust: peculiarities you need to know

Rust is a systems programming language, but also a high-performance, reliable, and safe option for web development as well. A crucial step in the evolution of the language was the emergence of web frameworks and libraries. This has made Rust a future technology for web development. A few important points that make this language exceptional:

  • Rust has a focus on safety, concurrency, and productivity
  • A memory-safe language that prevents bugs and security vulnerabilities 
  • Asynchronous support positively influences the speed of development
  • It does not use garbage collectors or virtual machines, which results in better execution times than, for example, Java,  C#, or Python.  

Further in our article, you will find more features of Rust language, but if you are interested in comparing Rust with another popular programming language – Go, you can read material about Rust vs Go.

Rust language: special features

  • Error handling. Rust allows you to create secure software quickly and easily. This is mainly achieved through error management in the compilation process. If some error occurs during compilation, it should be fixed by a developer and you can’t execute this program without fixing the error.
  • Secure memory management. The Rust compiler makes the “garbage collector” outdated, so instead, it used the ownership model and RAII conceptions. The memory is allocated when the scope starts and deallocated when the reference is out of scope. In that case, the compiler can check if you use a variable that is out of scope and no longer exists or if the value was moved to another scope.
  • Impressive performance efficiency and low memory consumption are the main selling points of Rust. It’s also speedy and allows you to have a higher level of abstraction without the cost.

    Rust is a general-purpose programming language inspired by other programming languages, including functional programming languages, and was designed to write high-load solutions, embedded applications, etc.

Let’s take a look at the next subtopic of our article – Rust’s use cases.

What is Rust used for?

Developers, mobile app and game creators, and more have been increasingly adopting the language. Rust ecosystem is incredibly suitable for numerous projects and enterprises due to its rapid pace of development. 

If you are going to build your next application using Rust, here is a list of the most common use cases for this language:

  • IoT solutions
  • Web services 
  • High-performance computing (HPC)
  • Advanced, cross-platform, command-line tools
  • Embedded devices
  • Game development
  • Distributed systems.

Speaking of what Rust is used for, it’s worth mentioning a code idiom RAII (Resource Acquisition Is Initialization): every time an object goes out of scope, its destructor is called and the resources it belongs to are freed. You don’t have to do it manually, and you are safe from resource leakage errors. This ultimately allows you to use memory more efficiently. Tilde, for example, used Rust language in their product Skylight. This allowed them to reduce memory usage from 5 GB to 50 MB.

Now, let’s dive into more technical features of the language.

Rust memory model

It is fair to say that the Rust memory model was inspired by the memory model used in C++. This memory model is an attempt to bridge the gap between the semantics you want to optimize and the optimizations that compilers want, and the inconsistent chaos that your hardware wants. The C++ memory model tries to close this void. To achieve a suitable level of safety, Rust employs “happens before” relationships (or causal relationships) between parts of the program and the threads that execute them. This allows the hardware and compiler to optimize more aggressively where a strict “will happen before” relationship is not established.

The borrow checker is a compiler feature that uses a type system and memory management features to ensure that you don’t access memory you don’t have permission to. If during runtime there is no more explicit guarantee that this resource will be available in some form or another, then we can say that the program has “broken”.

In Rust, we use a borrow checker by adding Lifetimes annotation to the code that allows us to talk about the kinds of data accesses, where they happen, who owns them, and when they must happen relative to other parts of your program. This gives both compilers and hardware room to make optimizations as long as those relationships aren’t violated—something which happens on occasion mostly because of a poor understanding of how things work at runtime.

Having dealt with the peculiarities of the memory model, let’s get closer to data collection in the Rust language.

Rust data collections

Rust includes several built-in container types. They allow you to store varying amounts of data, and they can grow or shrink as the program is running. Unlike an array or tuple, each container stores its data on the heap, which means that the total amount of memory occupied by a collection does not need to be known ahead of time. To help you learn about these collections and choose the appropriate one for your situation, here is a brief overview of the most common ones.

It can be helpful to remember about each collection as having three basic characteristics:

  • capacity (how many elements it can hold),
  • element type (how much storage each element uses),
  • access pattern (how quickly elements can be accessed).

Of course, knowing which collection is right for a particular job doesn’t mean you’ll be able to use it right away. If you’re interested in learning how to use a specific collection, check out its documentation for a detailed description and code examples. The Rust book can be helpful as a most popular official resource for learning Rust.

To summarize all of the above, remember Rust is a programming language designed to be secure, faster, and more reliable. The uniqueness of the technology has resulted in a proliferation of Rust frameworks and tools that make efficient web development faster and easier. To choose the best web framework, Rust engineers typically assess the options in terms of the technical problems they can solve. The more features a framework has, the wider its field of applicability.