Why is Rust more popular than Haskell

Rust is a programming language of Mozilla. Rust can be used, inter alia. Write command line tools, web applications and network programs. The language is also suitable for hardware-related programming. The language is extremely popular among Rust programmers.

In this Rust tutorial, we're going to show you how main features of the language. In doing so, we deal with similarities and differences to other common languages. We'll walk you through the Rust installation and learn to write and compile Rust code on your own system.

The Rust programming language at a glance

Rust is a compiled language. This property results in high performance; at the same time, the language offers sophisticated abstractions that make work easier for the programmer. Rust has a special focus on storage security. This gives the language a particular advantage compared to older languages ​​such as C and C ++.

Use Rust on your own system

Since Rust is free and open source software (FOSS), anyone can download the Rust toolchain and use it on their own system. Unlike Python or JavaScript, Rust is not an interpreted language. Instead of an interpreter, a compiler is used, as is the case with C, C ++ and Java. In practice this means that there are two steps to running code:

  1. The Compile source code. This creates an executable binary file.
  2. The resulting Run binary file.

In the simplest case, both steps are controlled via the command line.

In addition to executable binary files, Rust can also be used to generate libraries. If the compiled code is a directly executable program, a main () function To be defined. As in C / C ++, this serves as an entry point into the code execution.

Install Rust on the local system for the tutorial

In order to be able to use Rust, you first need a local installation. Under macOS use the homebrew package manager. Homebrew also works under Linux. Open a command line ('Terminal.App' on the Mac), copy the following line of code into Terminal and run it:

To Rust on Windows or another system without installing homebrew, use the official Rustup tool.

In order to verify that the Rust installation was successful, open a new window on the command line and run the following code:

If Rust has been installed correctly on your system, you will receive the Version of the Rust compiler displayed. If an error message appears instead, restart the installation if necessary.

Compile Rust Code

To compile Rust code, you need a Rust source file. Open the command line and execute the following pieces of code. We will first create a folder for the Rust tutorial on the desktop and switch to the folder:

Next we create the Rust source code file for a simple "Hello, World" example:

Rust source code files end with the abbreviation .rs.

In conclusion, we will do the Compile the Rust source code and run the resulting binary:

Use in the command rustc rust-tutorial.rs && ./rust-tutorialto combine the two steps. You can recompile and run your program on the command line by pressing the up arrow key followed by Enter.

Manage Rust packages with Cargo

In addition to the actual Rust language, there are a number of external packages. These so-called Crates can be obtained from the Rust Package Registry. The Cargo tool installed together with Rust is used for this. The cargoCommand is used on the command line and allows not only installing packages but also creating new packages. Check that Cargo has been installed correctly:

Learn Rust Basics

To learn about Rust we recommend that you do the Try out code examples yourself. You can use the already created file rust-tutorial.rs use. Copy and paste a sample code into the file, compile it, and run the resulting binary file. For this to work, the example code must be within the Main()Function to be inserted!

You can also use the Rust Playground right in your browser to try out Rust code.

Instructions and blocks

Instructions are a basic building block of code in Rust. A Statement ends with a semicolon (;) and, unlike an expression, does not return a value. Multiple statements can be grouped in a block. As in C / C ++ and Java, blocks are delimited by curly brackets '{}'.

Comments in Rust

Comments are an important feature of any programming language. They are used both for documenting the code and for planning before the actual code is written. Rust used same comment syntax as C, C ++, Java and JavaScript: Any text after a double slash is interpreted as a comment and ignored by the compiler:

Variables and constants

We use the keyword 'let' in Rust to declare a variable. An existing variable can be declared again in Rust and then “overshadows” the existing variable. In contrast to many other languages the value of a variable cannot be changed easily:

To the value of a Mark variable as changeable afterwards, Rust brings along the 'courage' keyword. The value of a variable declared with 'mut' can be changed:

A constant is generated with the keyword 'const'. The The value of a Rust constant must be known at compilation. The type must also be specified explicitly:

The The value of a constant cannot be changed - a constant cannot be declared as 'mut'. Furthermore, a constant cannot be redeclared:

Concept of ownership in Rust

One of the key features of Rust is the concept of ownership. The property is closely linked to the value of variables, their "lifetime" and memory management of objects on the "heap" memory. If a variable leaves the scope, its value is destroyed and the memory is released. Rust therefore manages without "garbage collection", which contributes to high performance.

Every value in Rust belongs to a variable - the owner. It can only one owner for each value give. If the owner passes the value on, then he is no longer the owner:

Special care must be taken when defining functions: If a variable is passed to a function, the owner of the value changes. The The variable can no longer be used after the function has been called. A trick is used here in Rust: Instead of passing the value itself to the function, a reference is declared with the ampersand symbol (&). This allows the value of a variable to be “borrowed”. Here's an example:

Control structures

A fundamental characteristic of programming is that to make the program flow non-linear. A program can branch, and program components can also be executed multiple times. It is only through this variability that a program can really be used.

Rust has the control structures available in most programming languages. This includes the loop constructs 'For' and 'while', as well as the branches via 'If' and 'else'. Rust also has some special features. The 'match' construct allows patterns to be assigned, while the 'loop' instruction creates an endless loop. To make the latter practicable, a 'break' statement is used.


The repeated execution of a block of code using loops is also known as "iteration". Often the elements of a container are iterated over. Like Python, Rust knows the concept of the "iterator". An iterator abstracts the successive access to the elements of a container. Let's look at an example:

What if you want to write a 'for' loop in the style of C / C ++ or Java? So you want one Define the starting number and the ending number and run through all the values ​​in between. In Rust, as in Python, there is the "Range" object for this case. This in turn generates an iterator on which the 'for' keyword operates:

A 'while' loop works the same in Rust as it does in most other languages. A condition is set and the loop body is executed as long as the condition is true:

All programming languages ​​can use 'while' to create a To create an infinite loop. Usually this is a bug, but there are use cases that require it. Rust knows the 'loop' instruction for such cases:

In both cases the 'break' keyword can be used to break the loop.


Branching with 'if' and 'else' also works in Rust just like in comparable languages:

Rusts is more interesting 'Match' keyword. This fulfills a similar function to the 'switch' statement in other languages. Check out the function for an example cards_symbol () in the section "Compound data types" (see below).

Functions, procedures and methods

In most programming languages ​​are Functions a basic building block of modular programming. Functions are defined in Rust with the keyword 'fn'. There is no strict distinction between the related concepts of function and procedure. Both are defined in almost identical ways.

A function in the strict sense of the word returns a value. Like many other programming languages, Rust also knows it Procedures, d. H. Functions that do not return any value. The only fixed restriction is that the return type of a function must be specified explicitly. If no return type is specified, the function cannot return a value; then by definition it is a procedure.

In addition to functions and procedures, Rust also knows the methods known from object-oriented programming. A Method is a function that is tied to a data structure. As in Python, methods in Rust are defined with the first parameter 'self'. A method is called according to the usual scheme object.method (). Here is an example of the method surface(), bound to a 'struct' data structure:

Data types and data structures

Rust is a statically typed language. Unlike in the dynamically typed languages ​​Python, Ruby, PHP or JavaScript, in Rust the type of each variable must be known at compilation.

Elementary data types

Like most high-level programming languages, Rust knows some elementary data types ("primitives"). Instances of elementary data types are allocated on the stack memorywhat is particularly high-performance. The values ​​of elementary data types can also be defined using “literal” syntax. This means that the values ​​can simply be written out.

Data type Explanation Type annotation
Integer integer i8, u8, etc.
Floating point Floating point number f64, f32
Boolean Truth value bool
Character single unicode letter char
String Unicode string st

Although Rust is a statically typed language, the type of a value does not always have to be explicitly declared. In many cases the type can be derived from the context by the compiler ("Type inference"). Alternatively, the type is specified explicitly via type annotation. In some cases the latter is absolutely necessary:

  • The return type of a function must always be specified explicitly.
  • The type of a constant must always be specified explicitly.
  • String literals need special handling so that their size is known at compilation.

Here are a few vivid examples of that Instantiation of elementary data types with literal syntax:

Compound data types

Elementary data types map individual values, whereas Compound data types bundle multiple values. Rust provides a handful of compound data types to the programmer.

The instances of composite data types are, like instances of elementary data types, assigned on the stack. To make this possible, the instances must have a fixed size. It also follows from this that they cannot be changed at will after the instantiation. Here is an overview of the most important compound data types in Rust:

Data type Explanation Type of elements Literal syntax
Array List of several values same type [a1, a2, a3]
Tuple Arrangement of several values any type (t1, t2)
Struct Grouping of several named values any type
Enum enumeration any type

Let's first look at a 'struct'Data structure at. We define a person with three named fields:

To depict a specific person, we instantiate the 'struct':

An 'enum' (short for "enumeration", in German "Aufzählung") forms possible variants of a property from. We illustrate this here using a simple example of the four possible colors of a playing card:

Rust knows the 'match' keyword for "Pattern matching", in German "pattern assignment". The functionality is comparable to the 'switch' instruction in other languages. Here's an example:

A tuple is an arrangement of several values ​​that can be of different types. The individual values ​​of the tuple can be through Destructuring assigned to multiple variables become. If one of the values ​​is not required, the underscore (_) is used as a placeholder, as is common in Haskell, Python and JavaScript. Here's an example:

Since tuple values ​​are ordered, they can also be accessed via a numeric index possible. The indexing is not done in square brackets, but using a period syntax. In most cases, destructuring should make your code more readable:

Learn high-level programming constructs in Rust

Dynamic data structures

The compound data types already presented have in common that their instances are assigned on the stack. Rust's standard library also contains a number of commonly used dynamic data structures. Instances of these data structures are allocated on the heap. It follows that the size of the instances can be changed afterwards. Here is a brief overview of frequently used dynamic data structures:

Data type Explanation
Vector dynamic list of several values ​​of the same type
String dynamic sequence of Unicode letters
HashMap dynamic assignment of key-value pairs

Here is an example of a dynamically growing vector:

Object-oriented programming (OOP) in Rust

In contrast to languages ​​like C ++ and Java Rust has no concept of classes. Nevertheless, the OOP methodology can be programmed following. The data types already presented are the basis. In particular, the 'struct' type can be used to define the structure of objects.

In addition, there are “Traits” in Rust. A Trait bundles a lot of methodswhich can then be implemented of any type. A trait includes method declarations, but can also contain implementations. Conceptually, a trait lies somewhere between a Java interface and an abstract base class.

An existing trait can be implemented by different types. In addition, one type can implement multiple traits. Rust allows that Composing functionality for different typeswithout these having to inherit from a common ancestor.

Meta programming

Like many other programming languages, Rust allows you to write code for meta-programming. It refers to Code that generates further code. On the one hand, this includes the “macros” in Rust, which you may be familiar with from C / C ++. Macros end with an exclamation mark (!); You have come across the 'println!' macro for outputting text on the command line several times in this article.

On the other hand, Rust also knows “Generics”. These allow the writing of code that abstracts over several types. Generics are roughly comparable to the templates in C ++ or the generics of the same name in Java. A generic frequently used in Rust is 'Option ', which abstracts the duality 'None' / 'Some (T)' for any type 'T'.

Rust has the potential to replace the long-established favorites C and C ++ as a system programming language.

Similar articles

GitLab tutorial: Installation and first steps in GitLab

GitLab is one of the most popular tools in cross-team, agile software development. In this beginner-friendly GitLab tutorial you will find out everything you need to know to get started in GitLab: Among other things, we will show you the advantages of the software, how to install GitLab and how teamwork works.

Haskell tutorial: learning Haskell made easy

Anyone interested in the art of functional programming will find a worthy representative in Haskell. Learning the language is a big step for beginners - once the first hurdles have been overcome, progress can be made quickly. The following Haskell tutorial summarizes the most important steps, tips and tricks for getting started with Haskell.

DART tutorial - first steps very practical

The DART tutorial makes it easy for you to take the first steps to learn how to program with DART. If you are already familiar with other programming languages, you will notice that DART strives for simple and clear syntax in order to overcome some of the basic weaknesses of JavaScript. Go through the examples in our DART-Lang tutorial and try your hand at your own ...