Rust programming language: utilities, documentation, ideology and syntax. What is the status of the Rust language at the moment? Growing programming language

Already now you could notice that the syntax of the programming language in question is very similar to the syntax of languages ​​such as C / C ++, because in both cases two slashes are used to separate comments, code blocks are framed by curly braces, and function arguments are parentheses. Also keep in mind that the fn keyword is used to declare functions, and every program must have a main() function. The exclamation point after the println function name in this case indicates that a macro is being used (in fact, it is a convenient wrapper over the print function from the Rust runtime library).

To compile the program, simply run the command:

Rustc hello.rs

As a result, in the directory with the file source code program, a binary file with the name hello should appear, for the execution of which it is enough to execute the command./hello . But if you pay attention to the size of this file, you will be somewhat shocked: it will exceed 800 KB. And all this is needed for such a simple program to work? Well, by default the Rust compiler statically links most of the runtime libraries to the program, so you can copy the binary to a system that doesn't have the Rust runtime libraries installed and run it without any problems. However, you can also tell the compiler to perform optimizations and dynamic linking:

Rustc -O C prefer-dynamic hello.rs

Now you will get a more acceptable 8 KB binary file, but if you use the ldd utility, you will find that the libstd dynamic library is required for the program to work correctly.<версия>.so

Programming language syntax

Now that we can compile and run programs in Rust, I propose to understand the syntax of this programming language and highlight its differences from the syntax of such programming languages ​​as C, C ++ and other similar ones:

fn doubler (x: i32) -> i32 ( x * 2 ) fn main () ( let a: i32 = 5; let b; b = doubler(a); println!("a times 2 ()", b); match b ( 1 ... 10 => println!("1 to 10"), _ => println!("Another number"), ) )

If you are used to working with C/C++ languages, you might think that given code is somewhat strange, but it is quite logical. Let's start with the main() function: on the first line of let , we declare a 32-bit integer variable a and assign it an initial value of 5. We could skip specifying the type of the variable (i32 is the standard variable type), and also not assign it an initial value , and in this case it would contain the value zero. Note that if you declare a variable and assign a specific value to it in the same way as in the case of variable a in the example, you will not be able to change its value later, so when you compile the following code snippet, an error message will be generated:

Let a: i32=5; a = 10;

By default, variables in Rust are immutable, that is, their values ​​cannot change after initialization. You must explicitly declare mutable variables in the same way:

Let mut a: i32 = 5;

What is it for? This is extra work, is not it? Well, in fact it is true, but, on the other hand, this feature programming language helps to develop secure programs. You should only make mutable variables whose values ​​actually need to change. Rust forces you to be as verbose as necessary to describe how the program works as accurately as possible: the line above declares a signed integer variable a of exactly 32 bits, with the possibility of changing its value in the future.

Next, we call our doubler function with variable a as an argument and store the return value in variable b . Pay attention to the declaration of the doubler function, which is located at the beginning of the program code: it specifies the type of the function parameter (i32) and the type of the return value (i32) after the symbols ->. It's also easy to see that within the function, a single x * 2 operation is performed, without even a semicolon following it, as in a normal block of Rust code; what is going on there?

It turns out that you can return the value of a function either in the same way as in the C language, or simply by placing the expression in last line function code, as was done in this case. And since it's just an expression, you don't need to put a semicolon after it.

Let's go back to the main() function where we used the println!() macro to print the result; pay attention to the technique of substituting the value of a variable using the character sequence () . Finally, the example demonstrates the extremely useful "match" keyword of the Rust programming language, which allows you to significantly reduce the amount of code in case you need to perform a large number of if/else operations. In this case, 1 ... 10 is the declaration of a range of values ​​(from 1 to 10 inclusive), and the underscore character (_) matches all other values.

In Rust, the char string type allows the use of four-byte characters, that is, any Unicode characters, which means that the programming language has already been adapted at the design stage to work with different languages ​​and special characters. Another useful data type is a tuple, which is a collection of variables of different types:

Let x = (1, 2.0, "Hello");

In this case, an integer value, a floating point value, and a string value are placed in the same tuple. These values ​​are immutable and can be accessed in the same way:

Println!("()", x.2);

As a result, the value of the third element of the tuple x will be displayed, that is, the string "Hello" . As with regular arrays, which are also supported in Rust, tuple element numbering starts from zero. You can use tuples to return multiple values ​​from a function:

fn switch(input: (i32, i32)) -> (i32, i32) ( (input.1, input.0) ) fn main() ( let x = (10, 50); let y = switch(x) ; println!("(), ()", y.0, y.1); )

In this case, the function named switch() takes a tuple of two 32-bit integer values ​​and stores them in the input variable. However, it also returns a tuple with two integer values. Within this function, a simple expression is used that allows you to swap the elements of a tuple and return the resulting tuple.

The main() function creates a tuple named x containing the values ​​10 and 50, and a tuple named y containing the values ​​that were returned after the switch() function was called. Next, a simple display of the values ​​of the tuple on the screen (50, 10) is carried out.

Advice: If you're itching to get to grips with Rust's features on your own, we recommend you start by reading the official documentation, located at https://doc.rust-lang.org/book .

It was short description syntax and features of the Rust programming language; if you'd like to learn more about this programming language in a special series of articles, let us know!

So, we want to bring to your attention a recent birthday boy (on May 15, 2016 he turned one year old) - Rust. This is a universal programming language developed by Mozilla, whose three main principles are speed, safety and ergonomics. The creators themselves immodestly consider it one of the most likely successors to C/C++. According to a StackOverflow portal survey, Rust is the most preferred language by developers today. So, let's take a closer look at what it is.

Rust for beginner

I don’t want to deceive anyone, so the next responsible statement: Rust is quite difficult to learn. Firstly, this is due to the youth of the language and, as a result, a small amount of literature. Secondly, it will probably be even easier for a person who is far from programming to learn it than for someone familiar with other languages. So, for example, a ready-made IT specialist will be very annoyed by the need to prescribe the smallest operations, and the absence of inheritance as such in the language will simply baffle.

However, Rust is developing rapidly (a new release is released every 6 weeks), the community is growing, and finding information on the Internet is no longer difficult.

How to study

You can find almost everything you need on the official website. In addition, the community of Rust followers is very large and friendly, so you can always turn to IRC (there is a Russian section) and the official forum for advice. In addition, little by little books began to appear, including electronic ones. While it is difficult to assess their quality, but there is such a fact.

For those who have gone through the initial stage of acquaintance, a lot of useful material can be found on GiHub, including RFCs and commits. Alternatively, you can attend in person or at least watch a webcast of one of the Rust conferences scheduled for the second half of the year. Here is the calendar:

  • September 9-10 RustConf conference in Portland, USA;
  • 17 September RustFest European Community Conference in Berlin, Germany;
  • October 27 Rust Belt Rust conference in Pittsburgh, USA;

Well, to get to know those who consider Rust their calling, and at the same time ask them all the wisdom, you can contact the organizers and participants of meetings of Rust fans in Moscow.

Peculiarities

Slightly duplicating what was said earlier, let's highlight the main pros and cons of the Rust language.

Pros:

  • Safe work with memory;
  • High performance;
  • Algebraic data type;
  • Compilation predictability;

Minuses:

  • Some code redundancy;
  • The high intensity of language development and, as a result, the lack of good relevant literature for study;
  • The need to clearly and unambiguously prescribe parameters for compilation.

In fact, you quickly get used to the differences, such as replacing inheritance with abilities. As soon as the eyes are accustomed, the hands are full, Rust turns into a completely working language, simpler and more functional than C ++, but inferior in “beautifulness” to many other programming languages. In fact, the main difference between Rust and its competitors and predecessors is speed and security.

Demand

Today, Rust is popular among game, graphics, and operating systems. However, for obvious reasons, the number of stationary places where highly specialized Rust experts would be required in the world, and even more so in Russia, is extremely small. Nevertheless, there are no signs yet that the language will sink into oblivion, more like a systematic takeover of the world. This means that good skills in using Rust in the future will help you find a highly paid interesting job both in our country and abroad.

Still in demand: profession "".

In 2013, Mozilla, together with Samsung, announced the development of a new Servo web browser engine. It was created specifically for multi-core processors. mobile devices, is able to split tasks into parallel threads and greatly reduce the loading time of web pages. Servo is written entirely in the Rust programming language, which Mozilla developed in-house for writing mobile applications.

About language

Rust is a procedural programming language that supports a variety of coding styles. Developer Graydon Hore began creating the language in 2006, and Mozilla joined the project three years later. In 2010, Rust was presented at the Mozilla Summit conference. In the same year, development was transferred to a compiler written in Rust. The compiler used the universal LLVM program analysis and transformation system as a database.

First stable version language was released in 2015. After the release of the alpha version, Rust underwent changes - only ready-made features were left inside the compiler that will not change. Everything else was moved to the experimental section.

At the heart of the language Graydon Hoare laid such concepts as:

  • Safety. Rust contains a number of programmer restrictions that are enabled by default. To disable them in blocks and functions, the "unsafe" label is required.
  • Speed. The language is comparable in speed to the other C++ programming language, which provides a clear number of advantages for the programmer.
  • Parallelism. The system can perform several calculations at the same time, at the same time they can interact with each other.
  • brevity. First keywords in Rust fit into five characters. But later this restriction was lifted.

An example of one of the first codes in Rust

However, Rust is not without its downsides, the most striking of which are:

  • Code redundancy.
  • Lack of literature for language learning.
  • Clarity in making compilation options. This does not always suit experienced programmers, since other languages ​​do not have such rules.

However, the language is regularly modernized and supplemented: its updates are released every 6 weeks.

Comparing Rust to C++

The creators of Rust consider it the successor to C++, which originated in the early 1980s when the developer came up with several improvements to the C language. Therefore, it is worth comparing the young and still changing language with the time-tested.

  • Accessing remote memory. In C++, deleting a variable can cause a number of problems. Such complications are not possible in Rust, since there are no commands for deleting memory. The descendant compiler will report that the code you have written contains an error, and the C++ compiler will print the result without removed values ​​without even reporting the problem.
  • Semicolon. Adding an extra semicolon to your code will cause an error in C++, whereas in Rust the loop body is enclosed in curly braces.
  • Unsafe code. Rust has an "unsafe" label that isolates the main code from unsafe. In the future, when reviewing the code, this allows you to narrow down the search for vulnerabilities.

It was in C++ that Firefox was implemented: this whimsical language required increased attention to detail. Otherwise, the errors turned into serious vulnerabilities. Rust was designed to deal with this problem.

prospects

In the RedMonk ranking for the third quarter of 2018, Mozilla's programming language is consistently ranked 23rd. Experts believe that he is not in danger of improving his positions. Despite this, in August 2018, the creators released an updated Rust 1.28.

After the release of Rust in 2015, according to the Stack Overflow site, 74% of developers wanted to get acquainted with it. However, already in 2016, it moved to the first place: 79% of users named Rust as their favorite programming language and expressed a desire to continue working with it. Rust took the first place in this parameter in 2018.

Stack Overflow is a popular programming question and answer system developed in 2008.

The popularity of Rust is confirmed by the number of companies using it in their developments. Currently, this list includes 105 organizations.

Rust develops steadily, new features and fixes are introduced with each release every 6 weeks. The noticed bugs are also fixed promptly in irregular minor releases. Sometimes such development dynamics can even serve as an obstacle: many "living" libraries require new version compiler, but not every company is able to quickly update it on their projects.

The infrastructure around Rust, while evolving, is still raw. Many libraries, although they already work quite stably, still require minor improvements in real use. If you are ready to fork such libraries on GitHub and slightly modify them to suit your needs, then I think you should not have any special problems using Rust in combat projects.

As far as I know, there is no single collection of best practices for using Rust. A lot of useful tips is in the official documentation (in the so-called Books), and also scattered in various separate articles. However, there are lists of useful articles that will help you find the right one among them. For example these:
https://github.com/ctjhoa/rust-learning
https://github.com/brson/rust-anthology/blob/maste...

Rust is used in new projects, and so far the trend is towards expansion. Here on this page you can see which companies are using Rust now and why: https://www.rust-lang.org/en-US/friends.html

So, if you are planning to use Rust in production, get ready for this:

  1. Pretty high entry threshold. There is no particular difficulty here, it just takes practice in the language and at first time to follow the advice of the compiler to eliminate constantly occurring compilation errors.
  2. Enough frequent updates compiler to add new features to the language. This may cause the library you need to require the latest version of the compiler.
  3. Raw libraries. You will probably have to modify them slightly for yourself.
  4. Rust simplifies the complex, but complicates the simple. For very simple projects that do not require high performance and serious improvements in the future, Rust may not be the best choice.
But what will you get from using Rust?
  1. High program performance automatic control memory without a garbage collector.
  2. High reliability and security of programs, elimination of a large number potential compile-time problems.
  3. A fairly easy and safe process of refactoring and refining programs, thanks to the developed type system.
  4. Developed project dependency management system.
  5. Really good all-around tool: Rust is good for both prototyping and development, and for any type of program (utilities, desktop apps, web apps, mobile applications, embedded systems). Good support is not yet available for everything, but in the future it is a big plus.