What is the Rust programming language?

According to the official page of the language:

Rust is a systems programming language that runs incredibly fast, prevents segmentation failures, and ensures security between threads.

It is relatively new, from 2010, and seems to be almost unknown or ignored by our region-since the tag rust did not even exist here on the site until this question.

As a way of understanding the context of language:

  • What is Rust?
  • at many times, Rust is compared to C++. Did it come up as an alternative to C++? What are the main differences between them?
  • The Expression "Systems programming" (in English, systems programming) is also mentioned. What does that mean?
Author: Jefferson Quesado, 2017-10-26

2 answers

Rust is a programming language primarily focused on:

  • Safety without garbage collector.

  • Competition without data dispute.

  • Abstraction without overhead.

Its design enables the creation of programs that have high performance and control of a low-level language, but with the powerful abstraction of a high-level language. These properties make Rust suitable for programmers who have experience in languages like C and are looking for a safer alternative, as well as those who come from languages like Python looking for ways to write code that runs better without sacrificing expressiveness.

Rust performs most security measures and memory control decisions at compile time, making execution performance unaffected. This is useful in a number of cases where other languages are not good: programs with predictable space and time requirements, inserted into other languages, and write low-level code, such as device drivers and operating systems.


In Rust there are no NULL pointers or loose pointers, making segmentation failures impossible. Rust manages memory and resources automatically, without requiring a garbage collector .

The language prevents data runs between threads because it is not possible that two threads can modify the same value at the same time. In order for a reference to be shared between multiple threads, it must be read-only . There are several secure techniques of communication between threads.

The principle of abstraction without overhead comes from C++. In the words of Bjarne Stroustrup :

"you don't pay for what you don't use. And more: what you usa, I couldn't program better to hand".

Rust allows a high degree of abstraction through the system of traits, which are interfaces that can be implemented separately from the declaration of a type. Generic types are used widely.


Example code using a simple algorithm to read and write a number:

use std::io;

fn main() {
    println!("Guess the number!");

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin().read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);
}

Function returning a number:

fn five() -> i32 {
    5
}

fn main() {
    let x = five();

    println!("The value of x is: {}", x);
}

More code examples here .

Sources: Rust Official E-Book , Wikipedia .

 10
Author: Zulian, 2017-10-26 13:30:22

TL; DR

What is Rust?

A programming language that values efficiency (zero-cost abstraction whenever possible) and security (of types, memory and sharing) while maintaining a certain degree of usability.

At many times, Rust is compared to C++. Did it come up as an alternative to C++? What are the main differences between them?

Yes, Rust would be a modern and legacy-free C++, but not 100%, Rust doesn't try being guides objects and does not take meta-programming to the extreme. The basic differences below.

The Expression "Systems programming" (in English, systems programming) is also mentioned. What does that mean?

Systems, in this context, oppose applications. We're talking about basic things like operating systems and drivers, or even bare metal programming embedded in devices that don't even use SOs. It also comes in software that they are not used directly by the user and that need to be very efficient because they are the most concrete side that applications access, platforms, mainly we are talking about server software such as databases, file systems , GUI desktop managers, HTTP processors and other network protocols, browsers, compilers, etc.

Details

Official pages are always marketers. "Incredibly fast" is subjective. Ruby it also runs fast, enough to make millions of calculations in a single second.

A called elevator pitch of Rust is the type safety, memory, and data run .

Type Security

Contrary to what many static typing languages preach and which consider themselves safe, this is often far from true, it does not apply to C++, C# or Java, they are all insecure in some points. Rust always ensures that the type is the correct at compile time. Which obviously makes it a little less flexible. Of course, you can explicitly say that you want the guy to pass for another, but this needs to be valid, there is no invalid coercion.

Memory Security

Rust ensures that memory is never accessed in an invalid state (it guarantees to boot correctly and can only access what is active) and never lets memory leak. All this with a system of referrals, loans, and types of memory management with single reference (Box<T>), with reference count (Rc<T>) and the same in atomic form (Arc<T>), plus some helpers such as Weak<T> to avoid cyclic references. At that point Rust is not 100% safe, but it is if you take some care in extreme cases, and if it fails there will only be memory leakage, but that is a little used Point.

Rust collects memory garbage automatically, it just doesn't use a tracing mechanism to do it. But it can even do, the language is prepared for it and has community initiatives to deliver something in this sense. What is not commented Is that often this mechanism can be less efficient than a good tracing garbage collector.

There is no problem writing to memory concurrently between threads, but this can only be done safely, the compiler can determine if this is happening.

Safety on competition

And finally it provides ways that a data is not accessed concurrently causing some negative impact, the so-called Data race. I can't say if it works 100%, I just see a lot of people saying yes, but I'm almost sure it's only if I take some care. The point is that you are not caught by surprise as it happens in other languages.

Almost 100% guaranteed

100% is very difficult and can create huge complicates for the language and for code. They already say that Rust is a complicated language to learn and it itself has difficulties to be implemented, so its compiler is one of the slowest on the market, which goes a little against the idea of being fast, at least in this sense (they say they will improve and of course they have made some efforts, but there is no miracle, if you have a very complex graph analysis does not have much to do, it can only improve a little the internal structures and algorithms to improve some points, and who knows how to use some tricks, like D does that turns off the garbage collection, so it has gain in allocation (a tracing GC usually allocates faster) and does not pay the price of release (but this prevents the compiler from being used as a service unless you implement a memory region system in it.

These guarantees are not valid when using a unsafe context required for some operations efficiently, where you can have access to null pointers, and access as weak typing.

Abstraction level

It is a high-level language with some low-level features. It has innovative and very interesting mechanisms. They say that there are some of the best theorists about programming languages , without losing the pragmatism of a market language, so much so that you probably already run some Rust-generated binary, for example in parts of Firefox. Mozilla funds language.

C++

It does not replace any language, but is an alternative to C++, it was created for this. Most C++ programmers do not care about Rust and manage to demonstrate that certain complications are not worth the effort. There are those who argue. I find the language quite interesting, but it's not all that some people say, and in certain scenarios I see more complex codes than they should, and to get efficiency need to write more, in some cases to achieve the who wants to need to write verbose codes and that seem meaningless(it does within the philosophy of language to force you to think and explain certain things before doing).

Rust is expressive, but not so much. It requires some boiler plate to give the guarantees it proposes.

Zero abstraction

There is the myth of her having zero abstraction. She seeks this, but does not always comply. I'm not saying it's something bad, but there are some cases that you pay for something you don't accurate, there are several criticisms on the subject and recognition from the community that it has even. It's a price to pay for having a little more usability. C++ has achieved some results in newer (future) versions by learning from past mistakes (hers and others) and by allowing more forms of abstractions than Rust.

Rust X C++

The list of differences for C++ is extensive to post here where you have several questions, some:

  • It controls the data Life Time.

  • Controls concurrent access naturally.

  • Memory management mechanisms are more flexible and robust.

  • Except for unsafe you can not escape the guarantees, including does not promote types automatically.

  • Has no commitment to C and its defects.

  • Generics is not as powerful as templates , but it's much easier and safer.

  • Traits is very strong, but C++ allows all this more generally, it may not be as beautiful and guaranteed, but this can change in C++20 and 23.

  • Obviously the library is completely different.

  • All of this C++11 News going forward isn't usually in Rust. Often because you do not need or have something better, or at least different.

  • Rust is not object-oriented, it only has struct and the implementation of methods that use this structure, in addition to the traits, has no classes, constructors, those things.

  • Usually you don't access pointers directly.

  • Rust has no exceptions, because it is a problematic mechanism and already considered archaic. But in some cases the exception can be useful for performance, so Rust loses to C++, even C # or Java in this specific cases.

  • Rust does not access data tracks without limit checking, which can greatly worsen performance if the compiler is unable to safely elide this check.

  • Rust has pattern matching.

  • And more ...

      E você deve reescrever tudo o que tem em Rust.
      </meme>
    

Rewrite in Rust book (satyrical)

C++ has evolved impressively and over time some of these differences may disappear.

 14
Author: Maniero, 2020-07-14 15:59:48