What is the Rust programming language?

Asked

Viewed 3,224 times

23

According to the official page of language:

Rust is a system programming language that runs incredibly fast, prevents segmentation failures, and ensures cross-threaded security.

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

How to understand the context of language:

  • What is Rust?
  • At many times, Rust is compared to C++. It has emerged as an alternative to C++? What are the main differences between them?
  • The expression "programming systems" is always cited (in English, systems Programming). What does that mean?

2 answers

16

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++. It has emerged 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 does not try to be object-oriented and does not lead to meta-programming to the extreme. The basic differences below.

The expression "systems programming" is always cited (in English, systems Programming). What this means?

Systems, in this context, oppose applications. We’re talking about basic things, like operating systems and drivers, or even programming Bare metal embedded in devices that don’t even use Oses. It also includes software that is not used directly by the user and that needs to be very efficient because they are the more concrete side that applications access, platforms, mainly we are talking about server software 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 also runs ra pido, enough to make millions of calculations in a single second.

The so-called elevator pitch of Rust is the type safety, of memory, and of data race.

Type safety

Unlike many static typing languages that are considered safe, this is often far from true, it is not true for C++, C# or Java, all of which are insecure at some points. Rust always ensures that the type is correct at the time of compilation. This obviously makes it a little less flexible. Of course, you can explicitly say that you want the type to pass itself off as 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 ensures proper startup and can only access what is active) and never leaks memory. All with a reference system, loans, and memory management types with unique reference (Box<T>), with reference count (Rc<T>) and the same in atomic form (Arc<T>), in addition to some auxiliaries such as the Weak<T> to avoid cyclical references. At this point Rust is not 100% safe, but it is if you take some precautions in extreme cases, and if it fails there will only be memory leakage, but this is a little used point.

Rust collects the memory junk automatically, it just doesn’t use a mechanism tracing to do this. But you can even do it, 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’s no problem writing in memory concurrently between threads, but this can only be done safely, the compiler can determine if this is occurring.

Competition safety

And finally it provides ways that a data is not accessed concurrently causing some negative impact, the call data race. I can’t tell if it works 100%, I just see a lot of people saying yes, but I’m pretty sure it’s only if you take a few precautions. The point is that you don’t get caught off guard like in other languages.

Almost 100% guaranteed

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

Such warranties are not valid when using a context unsafe needed 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 theoreticians on programming languages, without losing the pragmatism of a market language, so much so that you probably already run some binary generated by Rust, for example in parts of Firefox. Mozilla funds the 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 can demonstrate that certain complications are not worth the effort. There are those who contest. I find the language very interesting, but it’s not all that some people say, and in certain scenarios I see more complex code than they should, and to get efficiency you need to write more, in some cases to get what you want you need to write verbose and seemingly meaningless codes (it does within the philosophy of language to force you to think and explain certain things before doing).

Rust is expressive, but not that expressive. She demands some Boiler Plate to provide the guarantees it proposes.

Zero abstraction

There is the myth of her having zero abstraction. She seeks it, but does not always comply. I’m not saying it’s a bad thing, but there are some cases where you pay for something you don’t need, there’s a lot of criticism about it and recognition from the community you actually have. It’s a price to pay for having a little more usability. C++ has achieved some result in newer (future) versions by learning from the mistakes of the past (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 lifetime of data.

  • Naturally controls concurrent access.

  • Memory management mechanisms are more flexible and robust.

  • Except for the unsafe cannot escape guarantees, even does not promote types automatically.

  • No commitment to C and his flaws.

  • Generics is not as powerful as templates, but it’s a lot 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 these novelties from C++11 forward are not usually in Rust. Often because you don’t need or have something better, or at least different.

  • Rust is not object oriented, it just has struct and the implementation of methods that use this structure, in addition to traits, no classes, builders, that sort of thing.

  • Usually you do not access pointers directly.

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

  • Rust does not access tracks of data without limit check, which can greatly worsen performance if the compiler cannot safely override 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.

  • 1

    Meme? https://memegenerator.net/img/instances/58532625.jpg

  • 3

    @Jeffersonquesado https://i.imgur.com/8acN5PG.png I’ve added more to chat: https://chat.stackexchange.com/transcript/message/44188030#44188030

12


Rust is a programming language focused primarily on:

  • Security 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 that come from languages like Python that look for ways to write codes that run better without sacrificing expressiveness.

Rust performs most of the security measures and memory control decisions at compile time, so that execution performance is not affected. This is useful in many cases that other languages are not good: programs with predictable space and time requirements, inserted into other languages, and writing low-level code such as device drivers and operating systems.


In Rust no null pointers or loose pointers, preventing segmentation failures. Rust automatically manages memory and resources, without needing a garbage collector.

Language prevents data runs between threads because it is not possible for two threads to modify the same value at the same time. In order for a reference to be shared across multiple threads, it must be read-only. There are several secure techniques for 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 do use, could not program better at 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 widely used.


Code example using a simple read and write algorithm:

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 examples of code here.

Sources: Rust Official E-book, Wikipedia.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.