Langague rust

Rust, verlan of programming languages

Designed by Mozilla to make a new browser, "Servo", the Rust language is available on Github and interests system programmers who see it as a possible successor to C++ in their field. The study of the language, however, let regret that it is not a true successor to C.

The word "rust" means red hair in old English and can be an allusion to the fox, symbol of Mozilla.

Rust could be the new C to which would have been added dynamic arrays, objects, pattern matching with mixing of different types, automatic memory management.

It is not the case, the latter is the only real advantage in this "new" language, which is a hackneyed of old principles with new reserved words and deliberately unreadable syntax to give a more scholarly air. There is no consistency in the language that seems to try to be different for the principle and not for efficiency, as we will see.

Demonstration by comparing the elements of Rust with those of C or C++ or with a new imaginary C completed by these new elements present in all recent languages​​.




Display in the console.

Rust

fn main() {      
  println!("Hello World!");  
}

C

void main() {      
  puts("Hello World!");  
}
 
Displaying a string.

Rust

let str = "why not make it complicated";
println!("str: {}", str);

C

char *str = "when it could be simple";
puts(str);
 
In Rust, all local variables are declared by let and are constants! We must add the mut specifier, to be able to reassign them...

Variable in Rust

let a = 1;
a += 1;   // Error! 

Constant in C

const int a = 1;
a += 1;   // Error! 
 
A 'mutable variable' in Rust is a variable (as the name indicates) in C!!!

Mutable variable in Rust

let mut b = 2;
b += 1;  // Ok

Variable in C

int b = 2;
b += 1;  // Ok
 
The type is determined by inference, but as it can be ambiguous (and therefore unsafe, it should be noted) the possibility of explicit declaration is added as an option. The coercive nature of the language would have required it the default syntax.

Explicit types in Rust

let b : int = 2;

let ch : char = 'x';

Explicit types in C

int b = 2;

char ch = 'x';
 
Tuples are mixed lists. An array has the same purpose in other languages.

Tuple in Rust

let t = (4, 5.0, false, "hello");

Mixed array in new C

array t = { 4, 5.0, false, "hello"};
 
The switch structure is renamed match to be modern. The bottom line is that it is more elaborate, as in Scala.

Match in Rust

let x : int = 1;
match x {
  0 => { println!("Not found"); } 
  1 => { println!("Found"); } 
  _ => { println!("Default"); } // Le reste
}

Switch case in C

int x = 1;
switch(x) {
  case 0: puts("Not found"); break;
  case 1: puts("Found"); break;
  println!("Default"); // Le reste
}
 
The for loop inspired by Python is the only case where Rust is simpler than C.

Rust

for i in range(0, 10) {
  ...
}

C

for(i = 0; i <= 10; i++) {
  ...
}
 
Rust uses a special structure for infinite loops, unnecessary in C.

Rust

let ctr = 0;
loop {
  ctr += 1;
  if(ctr == 1) {
    break;  
  }
}

C

int ctr = 0;
while(1) {
  ctr += 1;
  if(ctr == 1) {
    break;  
  }
}
 
The declaration of a function shows the slang aspect of Rust compared to C! Header more complicated with no reason, return simpler but unnecessarily less secure, where is the consistency?

Rust

fn mult(x: int, y: int) -> int {
  x * y
}

C

int mult(int x, int y) {
  return (x * y);
}
 
struct have the role of classes but methods are added separately, which is an option in C++.

Struct in Rust

struct Point {
    x: int,
    y: int,
}

impl Point {
    fn Start() -> Point {
        Point { x: 0, y: 0 }
    }
}

Class in C++

class Point {
    int x;
    int y;
    Point Start() {
       x = 0; 
       y = 0;
       return this;
    }
}

 

In conclusion, Rust was designed as a system language to replace C++ with a safer memory management, a point we do not deny. Was it necessary to achieve this change by making the syntax so obscure? The motivations of the authors are not clear because the defects of C, such as semicolons (an error according to the authors themselves) and curly braces are retained, but the simplicity of the declarations is not.
The possibilities of the elements of the language were also restricted to reduce the risk of errors. This is something that did not turned well in the past to Pascal or Modula.

Should we use Rust, so choose between it and Go or C++? We will put go aside because it rather competes with server languages ​​like Java, Python , PHP, the garbage collector is not always desirable and online modules is even less, and the lack of generics.
It remains to choose between the insecurity of C++ but with total freedom and the ability to achieve what you want, or deal with the abstruse syntax of Rust for a safer memory management.
I give my opinion in verlan : "Dema dingaccor yourto rencefepre". It is wiser.

Reference : Rust reference manual.

Programming and data languages Asm.js - Basic - C - C++ - C# - Dart - Eiffel - Go - Java - JavaScript - Julia - Pascal - PHP - Python - Ruby - Rust - Scala - Scriptol - TypeScript - HTML - XML - XAML - SQL