⬆️ ⬇️

Rust disciplining programming language

Hello, dear readers!



Life does not stand still, and here in O'Reilly we thought about publishing the first fundamental book on the Rust programming language:





')

Interested in this topic, we decided to submit for translation the translation of the review article about the Rust language published in December 2014. The article is slightly shortened, given that some of its passages are already outdated, but the author views the language well in the context of existing alternatives, emphasizes its (unconditional) merits and (conditional) shortcomings.



However, in order to make it even more interesting, we will leave a link to another article about Rust, published in one of our favorite Russian-language programming blogs, in the commentary to this article. For a start - go under the cat.







Disclaimer: The taste for programming languages ​​is a very subjective matter, like this post. Take it with healthy skepticism .



Recently, several new programming languages. Among them, I was particularly interested in Rust . Below I will share my impressions of Rust and compare it with several other programming languages.



Barrier in the study of Rust



I met Rust not on the first try. When learning this language there are several barriers, in particular:



  1. The language is changing rapidly . In Rust, there is no "generous lifelong dictator." The language is developed thanks to the contributions of the participants in the core team and community
  2. Given the first point, the Rust training materials are very scarce . There is a manual , other official documentation and the Rust by Example website are excellent resources. However, Rust is much more complicated. Often, we have to study RFCs, blogs, and even comments on Github in order to find the necessary information, and even if this information appeared just yesterday, it is still not completely certain. I look forward to a good, authoritative book on Rust, although, I bet, it will be voluminous.
  3. The ownership system and borrowing checker in Rust can confuse newbies . To ensure memory safety without garbage collection, Rust uses an intricate system of borrowing and possession. Often she scares away the neophytes.
  4. Rust compiler is very strict . I call Rust a disciplining language. Everything that is not quite clear for the Rust compiler, you should specify on your own, and at first you don’t know some of your intentions yourself. This learning barrier, coupled with everyone else, often leads to the fact that the first impression of Rust turns out to be discouraging.




Virtues



Rust has many virtues. Some of them are unique.



Memory safety without garbage collection



Perhaps this is the most important achievement of Rust. In low-level programming languages ​​that allow direct manipulation of memory, errors such as the use of freed memory (use-after-free) or memory leaks during execution are quite expensive. In modern C ++, the ability to deal with such things has improved, but they imply strict technical discipline (read: programmers continue to perform unsafe operations). Accordingly, in my opinion, in general, C ++ cannot fundamentally and reliably solve this problem.



Indeed, Rust programmers can write unsafe code in an unsafe block, but (1) this is done consciously and (2) unsafe blocks can only be a very small fraction of the entire code base, and they are strictly controlled.

The garbage collector is the most common tool for memory security. If you get along with GC, then you have plenty of options. However, the ownership system used in Rust provides not only memory security, but also data and resource security (see below)



RAII and resources



RAII (getting a resource is initialization) is a strange term, but it conveys well the idea embedded in it. In Wikipedia, we read that RAII works with objects allocated on the stack. Rust's owning system allows this principle to be applied to objects allocated on the heap. Due to this, the automatic release of resources - for example, memory, files, sockets - becomes well predictable and guaranteed at compile time.

There are similar possibilities in dynamic languages ​​like Python or Ruby, but they are not as powerful as Rust IMO.



Competition without data races



Rust ensures data security in competitive programming — that is, it ensures that at any given time, only a large number of readers or a single “recorder” can access the data.



Algebraic data type



In addition to the usual types (tuples and structures), Rust also provides enumerated types (here they are called "types-sums" or "variant types") and pattern matching. It's amazing that a language for system programming has such an advanced type system.



Composition takes priority over inheritance



In Rust, type composition is clearly preferred to inheritance. I belong to the camp where this fact is considered advantageous. With the support of generic types in Rust, the key role is played by types.



Disadvantages (conditional)



Everything should be very clear.



Rust is a disciplining language. The compiler needs to report everything very clearly, or it will swear until there are no unexplained moments. As a rule, this benefits the quality of the code, but may be redundant if we are talking about rapid prototyping or one-time tasks.



As a result: you have to write better and clearer code on Rust. Once this is learned, the rough edges can more or less disappear.



Garbage collection is secondary



Rust has the simplest garbage collector: Rc, reference counting and Arc, atomic reference counting without looping detection. However, these features do not work in the default language, and you will have to use the standard Rust memory management mechanisms (stack, & and Box) more often. If the memory problems in your application are not significant, then you will have to suffer a Rust-based memory security model, where the garbage collector is not used.



Expressiveness is not an end in itself



Rust language does not bother about the expressiveness and beauty of the code. In this regard, it is definitely not bad, but not as wonderful as you might like.



Relatively high entry barrier



In principle, Rust is not one of those languages ​​that can be quickly mastered and after a few weeks writing professional code. Perhaps Rust is more compact than C ++, but definitely more than many programming languages. Compared to other languages, it cannot be called too accessible. This can be a problem if your priority is the speed of learning a language.



Rust and other languages



Dynamic languages



Dynamic (scenario) languages ​​are located on the opposite end of the spectrum of programming languages ​​from Rust. Compared to Rust, writing code in dynamic languages ​​is usually faster and easier. I think dynamic languages ​​win Rust in such situations:







In such cases, you should not try to do everything perfectly. On the contrary, Rust, in my opinion, is better suited for:







In general, when the quality of the code is critical. Dynamic languages ​​help to write code faster at the initial stage, but later the work is slowed down: you have to write more tests, the development line is broken, or even production interruptions occur. The Rust compiler forces you to do many things right at compile time, when it is not so expensive to detect and fix bugs.



Go



Comparing these two languages ​​is an excellent reason to argue, but since I studied Go for a while, I’ll still share here my subjective impressions of it. Compared to Rust, this is what I like about Go:







Why I tied up with Go:







Nim



Nim (formerly called Nimrod) is a very interesting language. It compiles to C, so its performance is quite good. Outwardly, it resembles Python, a language I always like to program. It is a language with garbage collection, but it provides mild real-time support, and the very behavior of the garbage collector is more predictable. It has an interesting system of effects. In principle, I really like this language.



The biggest problem in his case is the immaturity of the ecosystem. The language itself is well developed and relatively stable, but at the present time this is far from enough for the programming language to succeed. Documentation, standard libraries, package repositories, supporting frameworks, community participation and third-party developers ... it's not easy to bring all this to readiness for production.



Without specialists who would be engaged in completion of the language in full time, this last stage can be very exhausting. Among the new programming languages ​​Nim still can not boast of serious support.

At the same time, I wish him success and still follow him.



Other



There are other languages ​​like Julia and D. Julia is a dynamic language with good performance and smooth C style calls. (If you like dynamic languages ​​and REPL, pay attention). Julia attracted everyone’s attention thanks to its numerical and scientific fields. Although it may become a general-purpose language, it seems to me that the development of this language is significantly influenced by the community of its founders.



D, at least initially, was an attempt to create "C ++, only better." His version 1.0 was released in 2007, so this language is not so new. This is a good language, but for objective reasons, it has not yet got accustomed: the case of splitting Phobos / Tango at an early stage, and ensuring memory safety primarily through garbage collection, and in the original positioning as a replacement for C ++.



Why do I think the chances of Rust are pretty high



Nowadays there are so many new programming languages. What, in my opinion, among them stands Rust? I will cite the following reasons:



This language for system programming



Embeddability is no easy task. Perhaps, it is solved literally in several languages, or even just two: C and C ++. (Perhaps that is why Skylight chose Rust to develop an extension for Ruby, although it was extremely risky.) It is noteworthy how well Rust managed to get rid of the execution time costs. This opens up a unique perspective for Rust.



No null



A null object / pointer (the so-called " billion-dollar error ") is a common source of runtime errors. There are only a few programming languages ​​in which null is missing, mostly functional languages. The fact is that getting rid of null requires a very advanced type system. As a rule, to cope with this at the syntactic level of a language, an algebraic data type and pattern comparison are required.



Low level language with advanced high level constructs



Being up to the core "language on bare metal" (at least theoretically ), Rust also offers many relatively high-level features, including the algebraic data type, pattern comparison, type, type inference, etc.



Strong community and practical relevance



The Rust community is very friendly and active. (Of course, this is a subjective impression). In addition, Rust has been used in some serious practical projects - it is, in particular, the compiler Rust, Servo , Skylight, etc. still at the stage of language development.



Until now - without major errors



At times, the development of a language or framework within the company may accidentally come to a standstill. Fortunately, the main Rust team is doing an excellent job so far. Keep it up, Rust!



Rust for web development



If Rust is a system programming language, is it suitable for web development? I am looking for an answer to this question.



Libraries and frameworks



First of all, some HTTP libraries must be ready for this. (This is discussed on the site “Are we web yet” ). The first rust-http library is out of date; her potential heiress Teepee is practically in anabiosis. Fortunately, Hyper seems like a good candidate. It is already accepted in Servo, a symbiotic project of Rust, I regard this as a blessing for the role of the HTTP library for Rust.



The standard Rust library does not yet support asynchronous I / O. For this purpose, you can use the mio external library, which provides non-blocking socket I / O. Support for green flows has been eliminated as part of simplifying I / O.



Several Rust web frameworks are being actively developed, including Iron and nickel.rs . It may take time before the situation settles.



Rust - language for the web?



Someday libraries and frameworks will be ready. The question is, is Rust itself suitable for web development? Are the low-level capabilities of Rust related to memory management and security not too complex?



I think in the end it all depends on what you expect from the project. Above, comparing Rust with dynamic languages ​​in short-term projects, I mentioned that in such cases the complexity of Rust may be unjustified. But if you expect the product to survive for a long time - say, six months or more - then Rust can be a good option.



Is Rust suitable for web startups?



What about startups? They require rapid prototyping and development cycles. This is a more ambiguous question, but I remain unconvinced: if you are counting on a long-term project, then it is important to choose the right programming language, and Rust deserves special attention. From a business point of view, a language that provides rapid prototyping offers significant advantages, while refactoring and eliminating bottlenecks can always be left for later. Engineering reality is such that the cost of refactoring is usually higher than it seems, and even if you shake up a lot of elements of your system, the code written sometime ago will still remain in some corner. For years to come.



Try Rust!



Still, I recommend to get acquainted with Rust. For now, roughness is possible with him, but I am sure that they will soon disappear.

Source: https://habr.com/ru/post/267203/



All Articles